Open markhobson opened 8 months ago
A similar issue exists for InputRequired
. Here's a workaround for that:
class MultivalueInputRequired(InputRequired):
"""
A validator that ensures input was provided and supports multivalued fields.
"""
def __call__(self, form: BaseForm, field: Field) -> None:
if field.raw_data and all(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)
This is a general design flaw with WTForms. There's many other ways to break Optional
/InputRequired
/DataRequired
.
The best way to clean this up, would be to change the API in 4.0 and expect individual Field
classes to deal with the implementation details of what it means for that field to be optional/required.
The
Optional
validator is used to short-circuit validation to allow empty field values. This works for single-valued fields but not for multivalued fields. For example, whenDateField
is used with separate day, month and year controls.Actual Behavior
Expected Behavior
This issue is due to
Optional
only considering the first raw data value.A custom validator can be used to work around this issue:
Although this functionality could easily be accommodated by the library's
Optional
validator.Environment