behai-nguyen / behai-nguyen.github.io

My GitHub page.
1 stars 0 forks source link

2023/04/06/wtforms-integerfield-0-value-bug #2

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Python: WTForms 3.0.1 IntegerField and InputRequired do not accept 0 as valid! | behai-nguyen software development learnings and documentation

0 is a valid integer value. In the latest version of WTForms, version 3.0.1, IntegerField and InputRequired don’t accept 0 as valid. This appears to be an ongoing issue dating back several years. I am proposing a patch, which seems to be working for me.

https://behai-nguyen.github.io/2023/04/06/wtforms-integerfield-0-value-bug.html

behai-nguyen commented 1 year ago

I ended up subclass InputRequired, and use InputRequiredEx in place of InputRequired:

class InputRequiredEx(InputRequired):
    def __call__(self, form, field):
        if field.raw_data and len(field.raw_data):
            return

        if self.message is None:
            message = field.gettext("This field is required.")
        else:
            message = self.message

        field.errors[:] = []
        raise StopValidation(message)