Fields are responsible for rendering and data conversion. They delegate tovalidators for data validation.
- Field 2 And 3 Types
- Field 2 And 3/8
- Field 2 And 3rd
- Field 2 And 3 Components
- 2 32 Field Artillery
- Field 2 And 3 Digit
Field definitions¶
Fields are defined as members on a form in a declarative fashion:
Perform to preserve his health and avoid unnecessary injury while in the field or when deployed. In order to be effective, preventive medicine measures must be an item of command interest. 1-2 TC 4-02.3 6 May 2015. The Policy Manual is replacing the Adjudicator's Field Manual (AFM), the USCIS Immigration Policy Memoranda site, and other USCIS policy repositories. The Policy Manual contains separate volumes pertaining to different areas of immigration benefits administered by the agency, such as citizenship and naturalization, adjustment of status,.
When a field is defined on a form, the construction parameters are saved untilthe form is instantiated. At form instantiation time, a copy of the field ismade with all the parameters specified in the definition. Each instance of thefield keeps its own field data and errors list.
The label and validators can be passed to the constructor as sequentialarguments, while all other arguments should be passed as keyword arguments.Some fields (such as SelectField
) can also take additionalfield-specific keyword arguments. Consult the built-in fields reference forinformation on those.
The Field base class¶
wtforms.fields.
Field
[source]¶Stores and processes data, and generates HTML for a form field.
Field instances contain the data of that instance as well as thefunctionality to render it within your Form. They also contain a number ofproperties which can be used within your templates to render the field andlabel.
Construction
__init__
(label=None, validators=None, filters=(), description=', id=None, default=None, widget=None, render_kw=None, _form=None, _name=None, _prefix=', _translations=None, _meta=None)[source]¶Construct a new field.
label – The label of the field.
validators – A sequence of validators to call when validate is called.
filters – A sequence of filters which are run on input data by process.
description – A description for the field, typically used for help text.
id – An id to use for the field. A reasonable default is set by the form,and you shouldn't need to set this manually.
default – The default value to assign to the field, if no form or objectinput is provided. May be a callable.
widget – If provided, overrides the widget used to render the field.
render_kw (dict) – If provided, a dictionary which provides default keywords thatwill be given to the widget at render time.
_form – The form holding this field. It is passed by the form itself duringconstruction. You should never pass this value yourself.
_name – The name of this field, passed by the enclosing form during itsconstruction. You should never pass this value yourself.
_prefix – The prefix to prepend to the form name of this field, passed bythe enclosing form during construction.
_translations – A translations object providing message translations. Usuallypassed by the enclosing form during construction. SeeI18n docs for information on message translations.
_meta – If provided, this is the ‘meta' instance from the form. You usuallydon't pass this yourself.
If _form and _name isn't provided, an UnboundField
will bereturned instead. Call its bind()
method with a form instance anda name to construct the field.
Validation
To validate the field, call its validate method, providing a form and anyextra validators needed. To extend validation behaviour, overridepre_validate or post_validate.
validate
(form, extra_validators=())[source]¶Validates the field and returns True or False. self.errors willcontain any errors raised during validation. This is usually onlycalled by Form.validate.
Subfields shouldn't override this, but rather override eitherpre_validate, post_validate or both, depending on needs.
form – The form the field belongs to.
extra_validators – A sequence of extra validators to run.
pre_validate
(form)[source]¶Override if you need field-level validation. Runs before any othervalidators.
form – The form the field belongs to.
post_validate
(form, validation_stopped)[source]¶Override if you need to run any field-level validation tasks afternormal validation. This shouldn't be needed in most cases.
form – The form the field belongs to.
validation_stopped – True if any validator raised StopValidation.
errors
¶If validate encounters any errors, they will be inserted into thislist.
Data access and processing
To handle incoming data from python, override process_data. Similarly, tohandle incoming data from the outside, override process_formdata.
process
(formdata[, data])[source]¶Process incoming data, calling process_data, process_formdata as needed,and run filters.
If data is not provided, process_data will be called on the field'sdefault.
Field subclasses usually won't override this, instead overriding theprocess_formdata and process_data methods. Only override this forspecial advanced processing, such as when a field encapsulates manyinputs.
process_data
(value)[source]¶Process the Python data applied to this field and store the result.
This will be called during form construction by the form's kwargs orobj argument.
value – The python object containing the value to process.
process_formdata
(valuelist)[source]¶Process data received over the wire from a form.
This will be called during form construction with data suppliedthrough the formdata argument.
valuelist – A list of strings to process.
data
¶Contains the resulting (sanitized) value of calling either of theprocess methods. Note that it is not HTML escaped when using intemplates.
raw_data
¶If form data is processed, is the valuelist given from the formdatawrapper. Otherwise, raw_data will be None.
object_data
¶This is the data passed from an object or from kwargs to the field,stored unmodified. This can be used by templates, widgets, validatorsas needed (for comparison, for example)
Rendering
To render a field, simply call it, providing any values the widget expectsas keyword arguments. Usually the keyword arguments are used for extra HTMLattributes.
__call__
(**kwargs)[source]¶Render this field as HTML, using keyword args as additional attributes.
This delegates rendering tometa.render_field
whose default behavior is to call the field's widget, passing anykeyword arguments from this call along to the widget.
In all of the WTForms HTML widgets, keyword arguments are turned toHTML attributes, though in theory a widget is free to do anything itwants with the supplied keyword arguments, and widgets don't have toeven do anything related to HTML.
If one wants to pass the 'class' argument which is a reserved keywordin some python-based templating languages, one can do:
This will output (for a text field):
Note: Simply coercing the field to a string or unicode will render it asif it was called with no arguments.
__html__
()[source]¶Returns a HTML representation of the field. For more powerful rendering,see the __call__()
method.
Many template engines use the __html__ method when it exists on aprinted object to get an ‘html-safe' string that will not beauto-escaped. To allow for printing a bare field without calling it,all WTForms fields implement this method as well.
Message Translations
gettext
(string)[source]¶Get a translation for the given message.
This proxies for the internal translations object.
string – A unicode string to be translated.
A unicode string which is the translated output.
ngettext
(singular, plural, n)[source]¶Get a translation for a message which can be pluralized.
singular (str) – The singular form of the message.
plural (str) – The plural form of the message.
n (int) – The number of elements this message is referring to
Properties
name
¶The HTML form name of this field. This is the name as defined in yourForm prefixed with the prefix passed to the Form constructor.
short_name
¶The un-prefixed name of this field.
id
¶The HTML ID of this field. If unspecified, this is generated for you tobe the same as the field name.
label
¶This is a Label
instance which when evaluated as a stringreturns an HTML for='id'>
construct.
default
¶This is whatever you passed as the default to the field'sconstructor, otherwise None.
description
¶A string containing the value of the description passed in theconstructor to the field; this is not HTML escaped.
errors
A sequence containing the validation errors for this field.
process_errors
¶Errors obtained during input processing. These will be prepended to thelist of errors at validation time.
widget
¶The widget used to render the field.
type
¶The type of this field, as a string. This can be used in your templatesto do logic based on the type of field:
flags
¶An object containing boolean flags set either by the field itself, orby validators on the field. For example, the built-inInputRequired
validator sets the required flag.An unset flag will result in False
.
meta
¶The same meta object instance as is available asForm.meta
filters
¶The same sequence of filters that was passed as the filters=
tothe field constructor. This is usually a sequence of callables.
Basic fields¶
Basic fields generally represent scalar data types with single values, andrefer to a single input from the form.
wtforms.fields.
BooleanField
(default field arguments, false_values=None)[source]¶Represents an type='checkbox'>
. Set the checked
-status by using thedefault
-option. Any value for default
, e.g. default='checked'
putschecked
into the html-element and sets the data
to True
false_values – If provided, a sequence of strings each of which is an exact matchstring of what is considered a 'false' value. Defaults to the tuple(False,'false',',)
wtforms.fields.
DateField
(default field arguments, format='%Y-%m-%d')[source]¶Same as DateTimeField, except stores a datetime.date.
wtforms.fields.
DateTimeField
(default field arguments, format='%Y-%m-%d %H:%M:%S')[source]¶A text field which stores a datetime.datetime matching a format.
For better date/time fields, see the dateutilextension
wtforms.fields.
DecimalField
(default field arguments, places=2, rounding=None, use_locale=False, number_format=None)[source]¶A text field which displays and coerces data of the decimal.Decimal type.
places – How many decimal places to quantize the value to for display on form.If None, does not quantize value.
rounding – How to round the value during quantize, for exampledecimal.ROUND_UP. If unset, uses the rounding value from thecurrent thread's context.
use_locale – If True, use locale-based number formatting. Locale-based numberformatting requires the ‘babel' package.
number_format – Optional number format for locale. If omitted, use the default decimalformat for the locale.
wtforms.fields.
FileField
(default field arguments)[source]¶Renders a file upload field.
By default, the value will be the filename sent in the form data.WTForms does not deal with frameworks' file handling capabilities.A WTForms extension for a framework may replace the filename valuewith an object representing the uploaded data.
Example usage:
wtforms.fields.
MultipleFileField
(default field arguments)[source]¶A FileField
that allows choosing multiple files.
wtforms.fields.
FloatField
(default field arguments)[source]¶A text field, except all input is coerced to an float. Erroneous inputis ignored and will not be accepted as a value.
For the majority of uses, DecimalField
is preferable to FloatField,except for in cases where an IEEE float is absolutely desired over a decimalvalue.
wtforms.fields.
IntegerField
(default field arguments)[source]¶A text field, except all input is coerced to an integer. Erroneous inputis ignored and will not be accepted as a value.
wtforms.fields.
RadioField
(default field arguments, choices=[], coerce=unicode)[source]¶Like a SelectField, except displays a list of radio buttons.
Iterating the field will produce subfields (each containing a label aswell) in order to allow custom rendering of the individual radio fields.
Simply outputting the field without iterating its subfields will result ina
- list of radio choices.
- class
wtforms.fields.
SelectField
(default field arguments, choices=[], coerce=unicode, option_widget=None, validate_choice=True)[source]¶ - class
wtforms.fields.
SelectMultipleField
(default field arguments, choices=[], coerce=unicode, option_widget=None)[source]¶ - class
wtforms.fields.
SubmitField
(default field arguments)[source]¶ - class
wtforms.fields.
StringField
(default field arguments)[source]¶ - class
wtforms.fields.
HiddenField
(default field arguments)[source]¶ - class
wtforms.fields.
PasswordField
(default field arguments)[source]¶ - class
wtforms.fields.
TextAreaField
(default field arguments)[source]¶
Select fields take a choices
parameter which is a list of(value,label)
pairs. It can also be a list of only values, inwhich case the value is used as the label. The value can be anytype, but because form data is sent to the browser as strings, youwill need to provide a coerce
function that converts a stringback to the expected type.
Field 2 And 3 Types
Select fields with static choice values:
Note that the choices keyword is only evaluated once, so if you want to makea dynamic drop-down list, you'll want to assign the choices list to the fieldafter instantiation. Any inputted choices which are not in the given choiceslist will cause validation on the field to fail. If this option cannot beapplied to your problem you may wish to skip choice validation (see below).
Select fields with dynamic choice values:
Note we didn't pass a choices to the SelectField
constructor, but rather created the list in the view function. Also, thecoerce keyword arg to SelectField
says that weuse int()
to coerce form data. The default coerce isunicode()
.
Skipping choice validation:
Note the validate_choice parameter - by setting this to False
weare telling the SelectField to skip the choice validation step and insteadto accept any inputted choice without checking to see if it was one of thegiven choices. This should only really be used in situations where youcannot use dynamic choice values as shown above - for example where thechoices of a SelectField
are determineddynamically by another field on the page, such as choosing a country andstate/region.
Advanced functionality
SelectField and its descendants are iterable, and iterating it will producea list of fields each representing an option. The rendering of this can befurther controlled by specifying option_widget=.
No different from a normal select field, except this one can take (andvalidate) multiple choices. You'll need to specify the HTML sizeattribute to the select field when rendering.
The data on the SelectMultipleField is stored as a list of objects, each ofwhich is checked and coerced from the form input. Any inputted choiceswhich are not in the given choices list will cause validation on the fieldto fail.
Represents an type='submit'>
. This allows checking if a givensubmit button has been pressed.
This field is the base for most of the more complicated fields, andrepresents an type='text'>
.
Convenience Fields¶
HiddenField is a convenience for a StringField with a HiddenInput widget.
It will render as an type='hidden'>
but otherwise coerce to a string.
HiddenField is useful for providing data from a model or the application tobe used on the form handler side for making choices or finding records.Very frequently, CRUD forms will use the hidden field for an object's id.
Hidden fields are like any other field in that they can take validators andvalues and be accessed on the form object. You should consider validatingyour hidden fields just as you'd validate an input field, to prevent frommalicious people playing with your data.
A StringField, except renders an type='password'>
.
Also, whatever value is accepted by this field is not rendered backto the browser like normal fields.
This field represents an HTML </span></code> and can be used to takemulti-line input.</p><h2>Field Enclosures¶</h2><p>Field enclosures allow you to have fields which represent a collection offields, so that a form can be composed of multiple re-usable components or morecomplex data structures such as lists and nested objects can be represented.</p><dt><em>class </em><code>wtforms.fields.</code><code>FormField</code><span>(</span><em>form_class</em>, <em>default field arguments</em>, <em>separator='-'</em><span>)</span><span>[source]</span>¶</dt><p>Encapsulate a form as a field in another form.</p><dt>Parameters</dt><ul><li><p><strong>form_class</strong> – A subclass of Form that will be encapsulated.</p></li><li><p><strong>separator</strong> – A string which will be suffixed to this field's name to create theprefix to enclosed fields. The default is fine for most uses.</p></li></ul><p>FormFields are useful for editing child objects or enclosing multiplerelated forms on a page which are submitted and validated together. Whilesubclassing forms captures most desired behaviours, sometimes forreusability or purpose of combining with <cite>FieldList</cite>, FormField makessense.</p><p>For example, take the example of a contact form which uses a similar set ofthree fields to represent telephone numbers:</p><p>In the example, we reused the TelephoneForm to encapsulate the commontelephone entry instead of writing a custom field to handle the 3sub-fields. The <cite>data</cite> property of the mobile_phone field will return the<code><span>data</span></code> dict of the enclosed form. Similarly, the<cite>errors</cite> property encapsulate the forms' errors.</p><dt><em>class </em><code>wtforms.fields.</code><code>FieldList</code><span>(</span><em>unbound_field</em>, <em>default field arguments</em>, <em>min_entries=0</em>, <em>max_entries=None</em><span>)</span><span>[source]</span>¶</dt><p>Encapsulate an ordered list of multiple instances of the same field type,keeping data as a list.</p><h3 id='field-2-and-38'>Field 2 And 3/8</h3><dt>Parameters</dt><ul><li><p><strong>unbound_field</strong> – A partially-instantiated field definition, just like that would bedefined on a form directly.</p></li><li><p><strong>min_entries</strong> – if provided, always have at least this many entries on the field,creating blank ones if the provided input does not specify a sufficientamount.</p></li><li><p><strong>max_entries</strong> – accept no more than this many entries as input, even if more exist informdata.</p></li></ul><p><strong>Note</strong>: Due to a limitation in how HTML sends values, FieldList cannot enclose<code><span>BooleanField</span></code> or <code><span>SubmitField</span></code> <a href='https://standardblog.mystrikingly.com/blog/sls-draw-reinssugars-legacy-stables'>Sls draw reinssugars legacy stables north port</a>. instances.</p><dt><code>append_entry</code><span>(</span><span>[</span><em>data</em><span>]</span><span>)</span><span>[source]</span>¶</dt><p>Create a new entry with optional default data.</p><p>Entries added in this way will <em>not</em> receive formdata however, and canonly receive object data.</p><dt><code>pop_entry</code><span>(</span><span>)</span><span>[source]</span>¶</dt><p>Removes the last entry from the list and returns it.</p><dt><code>entries</code>¶</dt><p>Each entry in a FieldList is actually an instance of the field youpassed in. Iterating, checking the length of, and indexing theFieldList works as expected, and proxies to the enclosed entries list.</p><p><strong>Do not</strong> resize the entries list directly, this will result inundefined behavior. See <cite>append_entry</cite> and <cite>pop_entry</cite> for ways you canmanipulate the list.</p><dl><dt><code>__iter__</code><span>(</span><span>)</span><span>[source]</span>¶</dt></dl><dl><dt><code>__len__</code><span>(</span><span>)</span><span>[source]</span>¶</dt></dl><dl><dt><code>__getitem__</code><span>(</span><em><span>index</span></em><span>)</span><span>[source]</span>¶</dt></dl><p><code><span>FieldList</span></code> is not limited to enclosing simple fields; and canindeed represent a list of enclosed forms by combining FieldList withFormField:</p><h2>Custom Fields¶</h2><p>While WTForms provides customization for existing fields using widgets andkeyword argument attributes, sometimes it is necessary to design custom fieldsto handle special data types in your application.</p><p>Let's design a field which represents a comma-separated list of tags:</p><p>The <cite>_value</cite> method is called by the <code><span>TextInput</span></code> widgetto provide the value that is displayed in the form. Overriding the<code><span>process_formdata()</span></code> method processes the incoming form data backinto a list of tags.</p><h3>Fields With Custom Constructors¶</h3><p>Custom fields can also override the default field constructor if needed toprovide additional customization:</p><p>When you override a Field's constructor, to maintain consistent behavior, youshould design your constructor so that:</p><ul><li><p>You take <cite>label='‘, validators=None</cite> as the first two positional arguments</p></li><li><p>Add any additional arguments your field takes as keyword arguments after thelabel and validators</p></li><li><p>Take <cite>**kwargs</cite> to catch any additional keyword arguments.</p></li><li><p>Call the Field constructor first, passing the first two positionalarguments, and all the remaining keyword args.</p></li></ul><h3>Considerations for overriding process()¶</h3><p>For the vast majority of fields, it is not necessary to override<code><span>Field.process()</span></code>. Most of the time, you can achieve what is needed byoverriding <code><span>process_data</span></code> and/or <code><span>process_formdata</span></code>. However, for specialtypes of fields, such as form enclosures and other special cases of handlingmultiple values, it may be needed.</p><p>If you are going to override <code><span>process()</span></code>, be careful about how you deal withthe <code><span>formdata</span></code> parameter. For compatibility with the maximum number offrameworks, we suggest you limit yourself to manipulating formdata in thefollowing ways only:</p><ul><li><p>Testing emptiness: <code><span>if</span><span>formdata</span></code></p></li><li><p>Checking for key existence: <code><span>key</span><span>in</span><span>formdata</span></code></p></li><li><p>Iterating all keys: <code><span>for</span><span>key</span><span>in</span><span>formdata</span></code> (note that some wrappers mayreturn multiple instances of the same key)</p></li><li><p>Getting the list of values for a key: <code><span>formdata.getlist(key)</span></code>.</p></li></ul><p>Most importantly, you should not use dictionary-style access to work with yourformdata wrapper, because the behavior of this is highly variant on thewrapper: some return the first item, others return the last, and some mayreturn a list.</p><h2>Additional Helper Classes¶</h2><dt><em>class </em><code>wtforms.fields.</code><code>Flags</code><span>[source]</span>¶</dt><p>Holds a set of boolean flags as attributes.</p><p>Accessing a non-existing attribute returns False for its value.</p><p>Usage:</p><dt><em>class </em><code>wtforms.fields.</code><code>Label</code><span>[source]</span>¶</dt><p>On all fields, the <cite>label</cite> property is an instance of this class.Labels can be printed to yield a<code><span><label</span><span>for='field_id'>Label</span><span>Text</label></span></code>HTML tag enclosure. Similar to fields, you can also call the label withadditional html params.</p><dt><code>field_id</code>¶</dt><p>The ID of the field which this label will reference.</p><dt><code>text</code>¶</dt><p>The original label text passed to the field's constructor.</p><h2>HTML5 Fields¶</h2><p>In addition to basic HTML fields, WTForms also supplies fields for the HTML5standard. These fields can be accessed under the <code><span>wtforms.fields.html5</span></code> namespace.In reality, these fields are just convenience fields that extend basic fieldsand implement HTML5 specific widgets. These widgets are located in the <code><span>wtforms.widgets.html5</span></code>namespace and can be overridden or modified just like any other widget.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>SearchField</code><span>(</span><em>default field arguments</em><span>)</span><span>[source]</span>¶</dt><p>Represents an <code><span><input</span><span>type='search'></span></code>.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>TelField</code><span>(</span><em>default field arguments</em><span>)</span><span>[source]</span>¶</dt><p>Represents an <code><span><input</span><span>type='tel'></span></code>.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>URLField</code><span>(</span><em>default field arguments</em><span>)</span><span>[source]</span>¶</dt><p>Represents an <code><span><input</span><span>type='url'></span></code>.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>EmailField</code><span>(</span><em>default field arguments</em><span>)</span><span>[source]</span>¶</dt><p>Represents an <code><span><input</span><span>type='email'></span></code>.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>DateTimeField</code><span>(</span><em>default field arguments</em>, <em>format='%Y-%m-%d %H:%M:%S'</em><span>)</span><span>[source]</span>¶</dt><p>Represents an <code><span><input</span><span>type='datetime'></span></code>.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>DateField</code><span>(</span><em>default field arguments</em>, <em>format='%Y-%m-%d'</em><span>)</span><span>[source]</span>¶</dt><p>Represents an <code><span><input</span><span>type='date'></span></code>.</p><h3 id='field-2-and-3rd'>Field 2 And 3rd</h3><dt><em>class </em><code>wtforms.fields.html5.</code><code>TimeField</code><span>(</span><em>default field arguments</em>, <em>format='%H:%M'</em><span>)</span><span>[source]</span>¶</dt><h3 id='field-2-and-3-components'>Field 2 And 3 Components</h3><p>Represents an <code><span><input</span><span>type='time'></span></code>.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>DateTimeLocalField</code><span>(</span><em>default field arguments</em>, <em>format='%Y-%m-%d %H:%M:%S'</em><span>)</span><span>[source]</span>¶</dt><p>Represents an <code><span><input</span><span>type='datetime-local'></span></code>.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>IntegerField</code><span>(</span><em>default field arguments</em><span>)</span><span>[source]</span>¶</dt><p>Represents an <code><span><input</span><span>type='number'></span></code>.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>DecimalField</code><span>(</span><em>default field arguments</em><span>)</span><span>[source]</span>¶</dt><p>Represents an <code><span><input</span><span>type='number'></span></code>.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>IntegerRangeField</code><span>(</span><em>default field arguments</em><span>)</span><span>[source]</span>¶</dt><h3 id='2-32-field-artillery'>2 32 Field Artillery</h3><p>Represents an <code><span><input</span><span>type='range'></span></code>.</p><dt><em>class </em><code>wtforms.fields.html5.</code><code>DecimalRangeField</code><span>(</span><em>default field arguments</em><span>)</span><span>[source]</span>¶</dt><h3 id='field-2-and-3-digit'>Field 2 And 3 Digit</h3><p>Represents an <code><span><input</span><span>type='range'></span></code>.</p><br><br><br><br>