I need to make a custom validator in WTForms where the input is to be: number:number - e.g. 2:1
match1 = StringField('Russia-Saudi Arabia', validators=[DataRequired()])
So, my question is - how to create such validator?
I've looked upon the documentation at http://wtforms.readthedocs.io/en/latest/validators.html, but was not very helpful (for me) in this case.
Thanks in advance
You can write a custom validator within a form by writing a
validate_{field_name}
method. If it raises aValidationError
, the form will not be valid and will display the error.For your specific case, here's a solution using regex. It finds the match for the string, and then uses a bit of splitting to get back the scores. After validating the form you can access the scores by
form.score1
,form.score2
.