watson.form.types

class watson.form.types.FieldDescriptor(name)[source]

Allow set/get access to the field value via Form.field_name

Fields accessed via a Form object will provide access directly to set and get the value of the field. When the field is being rendered in a template, the set/get will provide access to the field object itself. Access to the field prior to rendering can be made via Form.fields[name] where name is the attribute name of the field.

__init__(name)[source]
class watson.form.types.Form(name=None, method='post', action=None, detect_multipart=True, validators=None, values_provider=None, **kwargs)[source]

Declarative HTML <form> management.

Example:

from watson.form import fields
class MyForm(Form):
    text = fields.Text(name='text', label='My TextField')
    another = fields.Checkbox(name='another[]')

form = MyForm('my_form')
form.text = 'Something'

# in view
{% form.open() %}   # <form name="my_form">
{% form.text %}     # <input name="text" type="text" value="Something" />
{% form.text.render_with_label() %} # <label for="text">My TextField</label><input id="text" name="text" type="text" value="Something" />
{% form.another %}  # <input name="another[]" />
{% form.close() %}  # </form>
__init__(name=None, method='post', action=None, detect_multipart=True, validators=None, values_provider=None, **kwargs)[source]

Inititalize the form and set some default attributes.

Parameters:
  • name (string) – the name of the form
  • method (string) – the http method to use
  • action (string) – the url to submit the form to
  • detect_multipart (boolean) – automatically set multipart/form-data
bind(obj=None, mapping=None, ignored_fields=None, hydrate=True)[source]

Binds an object to the form.

Optionally additional mapping can be specified in order to set values on any of the classes that may exist within the object. If this method is called after the data has been set on the form, then the existing data will be overridden with the attributes on the object unless hydrate is set to false.

Parameters:
  • obj (class|dict) – the class to bind to the form.
  • mapping (dict) – the mapping between the form fields and obj attributes.
  • ignored_fields (list|tuple) – fields to ignore when binding.
  • hydrate (bool) – whether or not to hydrate the form with the obj attributes.

Example:

form = ...
user = User(username='test')
form.bind(user)
form.username.value  # 'test'
close(include_http_request=True)[source]

Render the end tag of the form.

If the form has the http_request_method input then include it in the tag by default.

Parameters:include_http_request (boolean) – Whether or not to include the HTTP_REQUEST_METHOD field
data

Returns a dict containing all the field values.

Used as a shorthand method to retrieve data from all the form fields rather than having to access the fields themselves.

invalidate()[source]

Invalidate the data that has been bound on the form.

This is called automatically when data is bound to the form and sets the forms validity to invalid.

is_valid()[source]

Determine whether or not the form and relating values are valid.

Filter all the values on the fields associated with the form, and then validate each field. Will only execute the filter/validation steps if the form has not been previously validated, or has been invalidated.

Returns:boolean value depending on the validity of the form.
open(**kwargs)[source]

Render the start tag of the form.

Any addition kwargs will be used within the attributes.

render(with_tag='div', with_label=True)[source]

Output the entire form as a string.

Called automatically by the __str__ method.

Parameters:
  • with_tag (string) – the tag to be used to separate the elements.
  • with_label (boolean) – render each field with it’s label.
Returns:

A string representation of the form.

class watson.form.types.FormMeta(name, bases, attrs)[source]

Assigns the FieldDescriptor objects to the Form object.

__init__(name, bases, attrs)[source]
class watson.form.types.Multipart(name, method='post', action=None, **kwargs)[source]

Convenience class for forms that should be multipart/form-data.

By default, the Form class will automatically detect whether or not a field is of type file, and convert it to multipart.

__init__(name, method='post', action=None, **kwargs)[source]