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

for Regexp validator set html input tag 'pattern' attribute #759

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions src/wtforms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ def __init__(self, regex, flags=0, message=None):
regex = re.compile(regex, flags)
self.regex = regex
self.message = message
self.field_flags = {"pattern": regex.pattern}

def __call__(self, form, field, message=None):
match = self.regex.match(field.data or "")
Expand Down
19 changes: 19 additions & 0 deletions tests/validators/test_regexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,29 @@ def test_regex_raises(re_pattern, re_flags, test_v, dummy_form, dummy_field):
validator(dummy_form, dummy_field)


def test_regexp_message_default(dummy_form, dummy_field, grab_error_message):
"""
Regexp validator should return default message
"""
validator = regexp("^a")
dummy_field.data = "f"
assert grab_error_message(validator, dummy_form, dummy_field) == "Invalid input."


def test_regexp_message(dummy_form, dummy_field, grab_error_message):
"""
Regexp validator should return given message
"""
validator = regexp("^a", message="foo")
dummy_field.data = "f"
assert grab_error_message(validator, dummy_form, dummy_field) == "foo"


def test_regexp_pattern_html(dummy_form, dummy_field):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure of the point of this test. I suppose the assertion could go on test_regex_passes.

"""
Regexp validator should return given message
"""
validator = regexp("^[a-zA-Z0-9]+$")
dummy_field.data = "foo bar"

assert validator.field_flags == {"pattern": "^[a-zA-Z0-9]+$"}