Wtforms Form With Custom Validate Method Is Never Valid
I want to get the data for a user input date, or the current date if nothing is entered. I used WTForms to create a form with a date input, and override validate to return True if
Solution 1:
You aren't overriding validate
correctly. validate
is what triggers the form to read the data and populate the data
attributes of the fields. You're not calling super
, so this never happens.
defvalidate(self):
res = super(ChooseDate, self).validate()
# do other validation, return final res
In this case there's no reason to override validate
, you're just trying to make sure data is entered for that field, so use the built-in InputRequired
validator. Using field validators will add error messages to the form as well.
from wtforms.validators importInputRequireddate= DateField(validators=[InputRequired()])
See the docs for more on validation.
Finally, you need to pass the form data to the form. If you were using the Flask-WTF extension's FlaskForm
, this would be passed automatically.
from flask importrequestform= ChooseDate(request.form)
Post a Comment for "Wtforms Form With Custom Validate Method Is Never Valid"