Python – Change field.data before calling DateField’s validator?

Change field.data before calling DateField’s validator?… here is a solution to the problem.

Change field.data before calling DateField’s validator?

My question is simple, here is a basic example:

class F(Form):
  date_test = DateField('Test', validators=[Required()], format='%d/%m/%Y')

I need to change the value sent by the user before calling the validator.
What is the easiest way to do it without losing the benefits of using WTForms?

Solution

All WTForm fields should support filters Keyword argument, which is the callable list that will be run on the input data:

def transform_data(data):
    # do something with data here
    return data

class F(Form):
    date_test = DateField('Test', validators=[Required()], format='%d/%m/%Y',
                              filters=[transform_data])

Related Problems and Solutions