WTForms validators.optional: continue checking empty fields?

I have a problem with WTForms validators.optional () because it stops the validation chain if the field is empty ( WTForms docs ). This means that validation is not performed using custom functions, which can lead to type errors.

Code example:

class MyForm(form): myfield = TextField('My Field', [validators.Optional()]) def validate_myfield(form, field): field.data = unicode(field.data) 

Is there a way or workaround to continue the validation chain, even if the optional content is empty, perhaps using custom validators ?

If I approach the problem incorrectly, a hint in the right direction will be helpful!

+4
source share
1 answer

You can simply change the order in which your validators are listed. If your custom validators are placed in front of an optional validator, it should provide the desired effect as they are evaluated in order.

+4
source

Source: https://habr.com/ru/post/1346138/


All Articles