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

Make Optional check on field.data instead of field.raw_data #843

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions src/wtforms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ def __init__(self, strip_whitespace=True):

def __call__(self, form, field):
if (
not field.raw_data
or isinstance(field.raw_data[0], str)
and not self.string_check(field.raw_data[0])
not field.data
or isinstance(field.data, str)
and not self.string_check(field.data)
):
field.errors[:] = []
raise StopValidation()
Expand Down
16 changes: 16 additions & 0 deletions tests/validators/test_optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,19 @@ def test_input_optional_raises(data_v, raw_data_v, dummy_form, dummy_field):
validator(dummy_form, dummy_field)

assert len(dummy_field.errors) == 0


def test_input_optional_raises_with_empty_formdata(dummy_form, dummy_field):
"""
optional should not stop the validation chain if field.data is not empty
"""
validator = optional()
dummy_field.data = "abc"
dummy_field.raw_data = None

dummy_field.errors = ["Invalid Integer Value"]
assert len(dummy_field.errors) == 1

validator(dummy_form, dummy_field)

assert len(dummy_field.errors) == 1
Loading