FlaskForm: automatically validate fields in a form?

Hello people.

I’m working on a CPython 3.10 project that uses flask_wtf.FlaskForm. I’m currently going through it with Vulture and removing dead code.

Our forms have a bunch of things like:

class CreateTransactionForm(FlaskForm):
    pipeline = IntegerField("pipeline", validators=[Required()])
    variant = IntegerField("variant", validators=[Required()])
    token = IntegerField("token", validators=[Optional()])
    type = IntegerField("type")

    def validate_type(form, field):
        if not VersionType.Exists(field.data):
            raise ValidationError("Unknown version type value")

That ‘validate_type’ method looks like it should be getting used to automatically validate the ‘type’ field - but I don’t think it is, because:

  1. Vulture identifies it as dead code.
  2. I added a ‘raise NotImplementedError’ to the beginning of each ‘def validate_*’, ran the software, and I got no new exceptions.

But we have a bunch of such methods, and the code looks like it might be nice to have - so rather than delete these methods, maybe there’s a good way of enabling their use?

My question is, can I somehow make such validate_foo methods get used for validating a WTForms ‘foo’ class variable? I’m assuming it involves inheritance and some form of metaclass or class introspection. I get inheritance, but I don’t know much about metaclasses and class introspection.

Any suggestions?


BTW, we’re still using WTForms 2.0.2 and Flask-WTF 1.0.1.

I’m realizing I can probably use the validators parameter.

That sounds much more direct than metaclass stuff.