Reputation: 509
What I would like to do is check what type of form validators a form object has so that I can render the field with based on those attributes. For example if the form has max-length validation then add the max-length property to the html form.
I was thinking of it working something like this:
zip_code = TextField('Zip Code', [validators.Length(min=5, max=11)])
will go to a Jinja2 macro like so:
{% if field.valdator "has max length" %}
{% set class = class + ' maxlength="11" ' %}
{% endif %}
{{ field(class=class, **kwargs) }}
Upvotes: 0
Views: 2239
Reputation: 4302
{{ form.field.validators }}
returns a list, so you can get validators by index value.
class ZipCodeForm(Form):
zip_code = StringField("Zip Code", [validators.Length(3,45)])
def register():
form = ZipCodeForm()
return render_template('register.html', form=form)
# register.html
...
{% macro textfield(type=text, max=20) %}
<input type={{type}}, size={{max}} >
{% endmacro %}
{% if form.zip_code.validators[0]["max"] %}
textfield(max=60)
{% endif %}
Upvotes: 5