watson.form.fields

class watson.form.fields.Button(name=None, value=None, default_value=None, label=None, label_attrs=None, **kwargs)[source]

Creates a button, can be used instead of Input(type=”button”).

class watson.form.fields.Checkbox(name=None, values=None, value=None, **kwargs)[source]

Creates a checkbox input.

Example:

field = Checkbox(name='test', label='My Radio Options', values=(('Test', 1), ('Testing', 2)))
str(field)
<fieldset>
    <legend>My Checkbox Options</legend>
    <label for="test_0">Test<input id="test_0" name="test" type="checkbox" /></label>
    <label for="test_1">Testing<input id="test_1" name="test" type="checkbox" /></label>
</fieldset>
field = Checkbox(name='test', label='My Checkbox', values=1)
str(field)=None
<label for="test"><input type="checkbox" name="test" value="1" />My Checkbox</label>
__init__(name=None, values=None, value=None, **kwargs)[source]

Initializes the checkbox.

If a value is specified, then that value out of the available values will be checked. If multiple values are specified, then a checkbox group will be created.

Parameters:
  • name (string) – the name of the field
  • values (tuple|list) – the values to be used
  • value (mixed) – the value for the field
class watson.form.fields.Csrf(name='csrf_token', value=None, **kwargs)[source]

Creates an <input type=”hidden” /> element for use in csrf protection.

__init__(name='csrf_token', value=None, **kwargs)[source]
class watson.form.fields.Date(name=None, value=None, format='%Y-%m-%d', **kwargs)[source]

Creates an <input type=”date” /> element.

__init__(name=None, value=None, format='%Y-%m-%d', **kwargs)[source]
render(**kwargs)[source]

Format the date in the format the HTML5 spec requires.

class watson.form.fields.Definition(class_, *args, **kwargs)[source]

Placeholder form element which allows for the creation of new form elements when the form is instantiated.

__init__(class_, *args, **kwargs)[source]
class watson.form.fields.Email(name=None, value=None, **kwargs)[source]

Creates an <input type=”email” /> element.

__init__(name=None, value=None, **kwargs)[source]
class watson.form.fields.FieldMixin(name=None, value=None, default_value=None, label=None, label_attrs=None, **kwargs)[source]

A mixin that can be used as a base to simplify the creation of fields.

When defining a field, a fully instantiated field must be created with definition=False as an argument in it’s __init__ method. This is to facilitate the way fields are defined in Form objects in 2.0.0.

label

watson.form.fields.Label – the label associated with the field

html

string – the html used to render the field

validators

list – the validators that will be used to validate the value.

filters

list – the filters that will be used prior to validation

__init__(name=None, value=None, default_value=None, label=None, label_attrs=None, **kwargs)[source]

Initializes the field with a specific name.

filter()[source]

Filter the value on the field based on the associated filters.

Set the original_value of the field to the first value stored. Note, if this is called a second time, then the original value will be overridden.

name

Convenience method to retrieve the name of the field.

original_value

Return the original value for the field.

render_with_label()[source]

Render the field with the label attached.

validate(form)[source]

Validate the value of the field against the associated validators.

Parameters:form (watson.form.types.Form) – The parent form of the field.
Returns:A list of errors that have occurred when the field has been validated.
value

Return the value for the field.

If the field has been cleaned, the original value can be retrieved with FieldMixin.original_value.

class watson.form.fields.GroupInputMixin(name=None, values=None, value=None, **kwargs)[source]

A mixin for form elements that are used in a group.

Related form elements are wrapped in a fieldset, with a common legend.

__init__(name=None, values=None, value=None, **kwargs)[source]
has_multiple_elements()[source]

Determine whether or not a field has multiple elements.

class watson.form.fields.Hidden(name=None, value=None, **kwargs)[source]

Creates an <input type=”hidden” /> element.

__init__(name=None, value=None, **kwargs)[source]
class watson.form.fields.Input(name=None, value=None, default_value=None, label=None, label_attrs=None, **kwargs)[source]

Creates an <input> field.

Custom input types can be created by sending type=’type’ through the __init__ method.

Example:

input = Input(type='text')  # <input type="text" />
render(**kwargs)[source]

Render the element as html.

Does not need to be called directly, as will be called by __str__ natively.

render_with_label(**kwargs)[source]

Render the element as html and include the label.

Output the element and prepend the <label> to it.

class watson.form.fields.Label(text, **kwargs)[source]

A <label> tag which can be automatically included with fields.

html

string – the html used to render the label

text

string – the text associated with the label

__init__(text, **kwargs)[source]
class watson.form.fields.Password(name=None, value=None, **kwargs)[source]

Creates an <input type=”password” /> element.

__init__(name=None, value=None, **kwargs)[source]
class watson.form.fields.Radio(name=None, values=None, value=None, **kwargs)[source]

Creates a radio input.

Example:

field = Radio(name='test', label='My Radio Options', values=(('Test', 1), ('Testing', 2)))
str(field)
<fieldset>
    <legend>My Radio Options</legend>
    <label for="test_0">Test<input id="test_0" name="test" type="radio" value="1" /></label>
    <label for="test_1">Testing<input id="test_1" name="test" type="radio" value="2" /></label>
</fieldset>
field = Radio(name='test', label='My Radio', values=1)
str(field)
<label for="test"><input type="radio" name="test" values="1" />My Radio</label>
__init__(name=None, values=None, value=None, **kwargs)[source]

Initializes the radio.

If a value is specified, then that value out of the available values will be checked. If multiple values are specified, then a radio group will be created.

Parameters:
  • name (string) – the name of the field
  • values (tuple|list) – the values to be used
  • value (mixed) – the value for the field
class watson.form.fields.Select(name=None, options=None, value=None, multiple=False, **kwargs)[source]

Creates a select field.

html

string – the html for the outer select element

option_html

string – the individual option html element

optgroup_html

string – the optgroup html element

options

list|dict – the options available

__init__(name=None, options=None, value=None, multiple=False, **kwargs)[source]

Initializes the select field.

If the options passed through are a dict, and the value of each key is a list or tuple, then an optgroup will be rendered, using the key as the label for the optgroup.

Parameters:
  • name (string) – the name of the field
  • options (list|dict) – the options available
  • value (string) – the selected value
  • multiple (bool) – whether or not to allow multiple selections

Example:

field = Select(name='test', options=collections.OrderedDict([('Group One', [1, 2]), ('Group Two', [1, 2])]))
str(field)
<select name="test">
    <opgroup label="Group One">
        <option value="1">1</option>
    </optgroup>
    <opgroup label="Group Two">
        <option value="2">2</option>
    </optgroup>
</select>
class watson.form.fields.Submit(name=None, value=None, button_mode=False, **kwargs)[source]

Creates a submit input.

button_mode

bool – whether or not to render as <button> or <input>

__init__(name=None, value=None, button_mode=False, **kwargs)[source]
class watson.form.fields.Text(name=None, value=None, **kwargs)[source]

Creates an <input type=”text” /> element.

__init__(name=None, value=None, **kwargs)[source]
class watson.form.fields.Textarea(name=None, value=None, default_value=None, label=None, label_attrs=None, **kwargs)[source]

Creates a textarea field.