Skip to main content

Generates render keywords for widgets of WTForms HTML5 fields.

Project description

WTForms HTML5

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

VERSION: 0.6.1

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.

Project details


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.1.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: wtforms-html5-0.6.1.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.1.tar.gz
Algorithm Hash digest
SHA256 39cb7aae903319ad618cb50b18937f96dcda1d800ab79bc519a942e271c9f32a
MD5 4ee5e36652d1333ddea3f856364fb4ba
BLAKE2b-256 f23fed8b340900b082563e3ebaead163a86144d41c2886f8b4e6e92960084a99

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wtforms_html5-0.6.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b256d36ba3b3eec3807dea62b2ad2fcea6984ec0e5910d020617ae494e210d6d
MD5 e98f65ffce3aa86c292f0227b2e04bd4
BLAKE2b-256 9db55bd439c9a3ae33fd33162f02eb9f548f017fb94cad21c63eac2bafe506a4

See more details on using hashes here.

Supported by

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