pallets-eco / wtforms

A flexible forms validation and rendering library for Python.
https://wtforms.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.51k stars 395 forks source link

Custom validators not working #853

Open MPanek6 opened 2 weeks ago

MPanek6 commented 2 weeks ago

Custom validators don't seem to fire. I've followed the documentation on the wiki as well as tried various online solutions

from wtforms import Form, StringField
from wtforms.validators import ValidationError

class xyzForm(Form):
    name = StringField("name")

    def validate_name(form, field):    # Using (self, field) or (self, form, field) also doesn't work
        print('fired')
        if len(field.data) > 2:
            raise ValidationError('blahblah')

Similarly, the following also doesn't work

from wtforms import Form, StringField
from wtforms.validators import ValidationError

def custom_val(form, field): 
    print('fired')
    if len(field.data) > 2:
        raise ValidationError('blahblah')

class xyzForm(Form):
    name = StringField("name", validators=[custom_val])

Environment