Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amend InputRequired validator to support multi-input fields #716

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Version x.x.x
-------------
Unreleased

- :class:`~validators.InputRequired` Validator interrogates data on entire
contents of field `raw_data`. :pr:`719`

Version 3.0.0
-------------

Expand Down
5 changes: 4 additions & 1 deletion src/wtforms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,17 @@ class InputRequired:
looks at the post-coercion data.

Sets the `required` attribute on widgets.

.. versionchanged:: 3.0,1
Validator interrogates data on entire contents of ``raw_data``.
"""

def __init__(self, message=None):
self.message = message
self.field_flags = {"required": True}

def __call__(self, form, field):
if field.raw_data and field.raw_data[0]:
if field.raw_data and any(field.raw_data):
return

if self.message is None:
Expand Down
10 changes: 7 additions & 3 deletions tests/validators/test_input_required.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
from wtforms.validators import StopValidation


def test_input_required(dummy_form, dummy_field):
@pytest.mark.parametrize(
"raw_data",
(["foobar"], ["", "foobar"]),
)
def test_input_required(dummy_form, dummy_field, raw_data):
"""
it should pass if the required value is present
"""
validator = input_required()
dummy_field.data = "foobar"
dummy_field.raw_data = ["foobar"]
dummy_field.data = "".join(raw_data)
dummy_field.raw_data = raw_data

validator(dummy_form, dummy_field)
assert validator.field_flags == {"required": True}
Expand Down