diff --git a/src/wtforms/validators.py b/src/wtforms/validators.py index 86337c99f..35ec77fdc 100644 --- a/src/wtforms/validators.py +++ b/src/wtforms/validators.py @@ -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 "") diff --git a/tests/validators/test_regexp.py b/tests/validators/test_regexp.py index 6d89857cd..72e0bf4b5 100644 --- a/tests/validators/test_regexp.py +++ b/tests/validators/test_regexp.py @@ -48,6 +48,15 @@ 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 @@ -55,3 +64,13 @@ def test_regexp_message(dummy_form, dummy_field, grab_error_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): + """ + 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]+$'}