Skip to main content

WTForms HTML5

PyPI Status Coverage Status pre-commit.ci Status Test Status

VERSION: 0.6.0

WTForms HTML5 generates render keywords for HTML5 INPUT widgets used by the WTForms library.

Evolution

Original Function: This module used to add support for the new HTML5 INPUT elements to WTForms. Besides supporting the new INPUT types, it also set some of the new attributes automatically, based on widget type and what kind of validators where set for the field.

Changes: WTForms — beginning around version 1.0.4 — started to implement some of these features. As of late 2016 the development version — that should become version 3 — has enough support for them IMO, so that to prevent the duplication of functionality, WTForms HTML5 dropped all the fields, widgets and validators — just use vanilla WTForms.

Current Function: starting with 0.2 versions of WTForms HTML5 merely contain one function: get_html5_kwargs — it adds the automatically generated keys to the render keywords of a WTForms field. A slim subclass of the new default Meta class for forms is also provided: AutoAttrMeta. If you use this class as your forms Meta, you get the automatic attributes for all fields in your form, just like in the original version of WTForms HTML5.

Supported Auto–Attributes

  • required

    Is set if the field has the required flag set. This happens i.e. if you use the DataRequired or InputRequired validator. The required attribute is used by browsers to indicate a required field (and most browsers won't activate the forms action unless all required fields have content).

  • invalid

    If the field got any validation errors, the CSS class invalid is added. The invalid class is also set by browsers, if they detect errors on a field. The validation errors detected by your code, are then by default styled in the same way as browser generated errors.

  • min / max and minlength / maxlength

    If either Length or NumberRange is used as a validator to set minimal and / or maximal values, the corresponding INPUT attribute is set (based on which validator is used). This allows for browser based validation of the values.

  • title

    If no title is provided for a field, the description (if one is set) is used for the title attribute.

Example

Declare your form just like in vanilla WTForms, but include AutoAttrMeta as your meta class:

>>> from wtforms import Form, StringField
>>> from wtforms.validators import InputRequired, Length
>>> from wtforms_html5 import AutoAttrMeta
>>> class MyForm(Form):
...   class Meta(AutoAttrMeta):
...     pass
...   test_field = StringField(
...     'Testfield',
...      validators=[InputRequired(), Length(min=3, max=12)],
...      description='Just a test field.',
...   )
>>> form = MyForm()

The only difference is, that you include a Meta class, that inherits from AutoAttrMeta.

This meta class sets the above mentioned attributes automatically for all the fields of the form:

>>> form.test_field()
'<input id="test_field" maxlength="12" minlength="3" name="test_field" required title="Just a test field." type="text" value="">'

The minlength and maxlength attributes are created because the Length validator was used. And the field is marked required because of the InputRequired validator. The field also gets a title taken from the fields description.

If you validate the form and any errors pop up, the field also get invalid added to its class:

>>> form.validate()
False
>>> form.test_field()
'<input class="invalid" id="test_field" maxlength="12" minlength="3" name="test_field" required title="Just a test field." type="text" value="">'

Install

You can install WTForms HTML5 with pip or from source.

pip is "a tool for installing and managing Python packages". If you don't have it installed, see the pip install instructions.

pip install --user wtforms-html5

Install Requirements

Since WTForms HTML5 only adds functionality to WTForms, you need to have it installed too. But if you use the installation method described above, it should have been taken care of.

Testing and Contribution

WTForms HTML5 is at home at: https://github.com/brutus/wtforms-html5/

You can run make setup after checkout, to setup a development environment.

If you find any bugs, issues or anything, please use the issue tracker.

Testing

You can run the provided doctest like this: make doctest. If you want to run the test cases, run make unittest (or make coverage). You can also run make all-tests to run this for all supported versions (this might take some time though).

If something fails, please get in touch.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wtforms-html5-0.6.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wtforms_html5-0.6.0-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file wtforms-html5-0.6.0.tar.gz.

File metadata

  • Download URL: wtforms-html5-0.6.0.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for wtforms-html5-0.6.0.tar.gz
Algorithm Hash digest
SHA256 af123aefefbcdbb513ffed30b29e37669f6db19667298fc3b2e54d6bbe723678
MD5 d3193edccc005bf9deca08722c124c1a
BLAKE2b-256 3bdcac00cce3bc55228846c5b022423a3daebeb660e82d2dde96089a6cca8a65

See more details on using hashes here.

File details

Details for the file wtforms_html5-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: wtforms_html5-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for wtforms_html5-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf64e5fc8b37a294a20416f9563d30b4b3b3e83ccb480ed864c3615c780c580a
MD5 3ef4a983309923d022d7abf0ef3e98dd
BLAKE2b-256 ebeb4791d451adb9eb915f13921b10a76174312f4786bafe43ddadb6b4dbdebb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page