Closed jb2170 closed 4 months ago
Can be achieved with validators = [..., NoneOf([None]), ...]
It's been a while since I've done Flask :smile:
But nonetheless charting out that table of permissibility helped me see the order of validation strictness :+1:
tldr: a validator for a non-optional (required) field that is allowed to be the empty string
An
Exists
validator class would check merely for the presence of the field name in the form, to make sure that a user has not forgotten the field exists, or has submitted data under the wrong field name.This is useful say for a field such as a picture
description
, which may be allowed to be an empty string, but we want to make sure that the user did indeed send an empty string, and that a non-empty string has not gone walkabout under the wrong field name ofdesc
, or the user forgot that adescription
field existed in the first place, which they would've liked to have filled in.This is particularly useful eg in building a JSON API which doesn't display a HTML form directly to the user, only validating the received form on the server side with WTForms.
Observe that
Exists
would fill in a gap in the truth table below, with validators increasing in order of permissibility.F
(False) denotes a failed validationT
(True) denotes the string passing the validatorDataRequired
InputRequired
Exists
Optional
Optional
is too permissiveInputRequired
is too strictImplementation
No-frills implementation:
Observe that we use
is None
, not just a loose false-y check. This the is essential difference fromInputRequired
.Full implementation: