开发者

WTForms validators.optional: continue validation of empty fields?

开发者 https://www.devze.com 2023-02-21 10:54 出处:网络
I have a problem with WTForms validators.optional() because it stops the validation chainif the field is empty (WTForms docs). This means that the validation does not continue with custom functions, w

I have a problem with WTForms validators.optional() because it stops the validation chain if the field is empty (WTForms docs). This means that the validation does not continue with custom functions, which can result in 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 any way or workaround to continue the validation chain even if the op开发者_如何学Pythontional content is empty, maybe using custom validators?

If I am approaching the problem in the wrong way, a hint at the right direction would be helpful!


You could just change the order in which your validators are listed. If your custom validators are placed before the optional validator it should provide the desired effect as they're evaluated in order.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号