Fields ====== .. module:: wtforms.fields Fields are responsible for rendering and data conversion. They delegate to validators for data validation. Field definitions ----------------- Fields are defined as members on a form in a declarative fashion:: class MyForm(Form): name = TextField(u'Full Name', [validators.required(), validators.length(max=10)]) address = TextAreaField(u'Mailing Address', [validators.optional(), validators.length(max=200)]) When a field is defined on a form, the construction parameters are saved until the form is instantiated. At form instantiation time, a copy of the field is made with all the parameters specified in the definition. Each instance of the field keeps its own field data and errors list. The label and validators can be passed to the constructor as sequential arguments, while all other arguments should be passed as keyword arguments. Some fields (such as :class:`SelectField`) can also take additional field-specific keyword arguments. Consult the built-in fields reference for information on those. The Field base class -------------------- .. class:: Field Stores and processes data, and generates HTML for a form field. Field instances contain the data of that instance as well as the functionality to render it within your Form. They also contain a number of properties which can be used within your templates to render the field and label. **Construction** .. automethod:: __init__ **Validation** To validate the field, call its `validate` method, providing a form and any extra validators needed. To extend validation behaviour, override `pre_validate` or `post_validate`. .. automethod:: validate .. automethod:: pre_validate .. automethod:: post_validate .. attribute:: errors If `validate` encounters any errors, they will be inserted into this list. **Data access and processing** To handle incoming data from python, override `process_data`. Similarly, to handle incoming data from the outside, override `process_formdata`. .. automethod:: process(formdata [, data]) .. automethod:: process_data .. automethod:: process_formdata .. attribute:: data Contains the resulting (sanitized) value of calling either of the process methods. Note that it is not HTML escaped when using in templates. **Rendering** To render a field, simply call it, providing any values the widget expects as keyword arguments. Usually the keyword arguments are used for extra HTML attributes. .. automethod:: __call__ If one wants to pass the "class" argument which is a reserved keyword in some python-based templating languages, one can do:: form.field(class_="text_blob") This will output (for a text field): .. code-block:: html Note: Simply coercing the field to a string or unicode will render it as if it was called with no arguments. **Properties** .. attribute:: name The HTML form name of this field. This is the name as defined in your Form prefixed with the `prefix` passed to the Form constructor. .. attribute:: id The HTML ID of this field. By default, this is auto-generated by appending the field's name to :attr:`~wtforms.form.Form._idprefix` .. attribute:: label This is a :class:`Label` instance which when evaluated as a string returns an HTML ``