Custom error message for invalid datetime format using flask wtform DateTimeField
I looked through WTForms' documentation and came up with nothing, so my suggestion is do a StringField with custom validator instead, like:
from datetime import datetimefrom flask_wtf import FlaskFormfrom wtforms import StringFieldfrom wtforms.validators import ValidationErrorclass NewShift(FlaskForm): start = StringField(u'Up and At Them') end = StringField(u'Another Day in the Office') def validate_start(form, field): try: datetime.strptime(field.data, '%d.%m.%Y %H.%M') except Exception: raise ValidationError('Wrong Input Format!') def validate_end(form, field): try: datetime.strptime(field.data, '%d.%m.%Y %H.%M') except Exception: raise ValidationError('Wrong Input Format!')
Then when you want to store the data, just do a strptime.