Skip to content

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

Merged
azmeuk merged 5 commits into
pallets-eco:mainfrom
lefty01:wtforms_validator_regex_set_html_attrib
May 8, 2026
Merged

azmeuk merged 5 commits into
pallets-eco:mainfrom
lefty01:wtforms_validator_regex_set_html_attrib

Conversation

@lefty01

@lefty01 lefty01 commented Nov 13, 2022

Copy link
Copy Markdown
Contributor

for Regexp validator set html input tag 'pattern' attribute (used if novalidate not set on a form)

Similar to the Length validator that sets the html input attributes minlength and maxlength I found it useful to have this for regex patterns as well.
One advantage of using the browser is to get the message translated already (even tough here it is a bit generic, since it only says input needs to match the requested format and does not tell about the format itself)

added some basic test ... not sure if that is accepted this way?

…novalidate not set on a form)

test that pattern shows up in the input field
@lefty01
lefty01 force-pushed the wtforms_validator_regex_set_html_attrib branch from 4b91b87 to b86c625 Compare November 13, 2022 19:09
@azmeuk

azmeuk commented Dec 24, 2022 •

Copy link
Copy Markdown
Member

Thank you for you contribution.
I think an additional test that actually generate some HTML would be good to have, maybe in the fashion of

def test_minlength_maxlength():

Also, can you add a CHANGELOG entry?

For the record, the related doc : https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern

Comment thread tests/validators/test_regexp.py Outdated
assert grab_error_message(validator, dummy_form, dummy_field) == "foo"


def test_regexp_pattern_html(dummy_form, dummy_field):

Copy link
Copy Markdown
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.

@lefty01

lefty01 commented Jan 2, 2023

Copy link
Copy Markdown
Contributor Author

... need to check again .. I was not that sure about proper testing here. will take a closer look Thx!

@azmeuk azmeuk added the enhancement New feature, or existing feature improvement label Jul 27, 2023
@azmeuk

azmeuk commented Jul 27, 2023

Copy link
Copy Markdown
Member

From the MDN documentation.

The pattern attribute is an attribute of the text, tel, email, url, password, and search input types.

You would probably want to add the pattern flag to the concerned widgets too.

@azmeuk

azmeuk commented Oct 10, 2023

Copy link
Copy Markdown
Member

I was thinking that python and JS regex might have different features: see here or here.

In that case, a valid python regex might not be a valid JS regex. There are compatibility layers like https://pypi.org/project/js-regex/ (but this one is unmaintained).

As differences mainly concerns advanced regex, i am not sure this is a problem.

@azmeuk

azmeuk commented Oct 10, 2023

Copy link
Copy Markdown
Member

@lefty01 are you interested in finishing the PR or would you like to let someone else take it over?

@ThiefMaster

Copy link
Copy Markdown

IMHO this should be configurable. You might not always want to send the pattern to the client, or you may want to use regex features not supported in the browser.

@lefty01

lefty01 commented Oct 11, 2023

Copy link
Copy Markdown
Contributor Author

@lefty01 are you interested in finishing the PR or would you like to let someone else take it over?

well, interested yes of course but right now it's not on top of my list ;)
so I'm quite happy there's now further discussion ongoing and of course do not mind at all if you/someone takes over the item so we can close it at some point.
... been a while and its bit hard to follow up anyways ;)

@dougthor42

Copy link
Copy Markdown

FYI: In my experience with HTML pattern, there are two caveats:

  1. I've found that the HTML pattern string needs to be slightly different than a standard python re.pattern string. IME the difference is typcially related to escaping.
    • Example: Python accepts r"[-\w]*", but HTML pattern must be r"[\-\w]*"
  2. It doesn't seem possible to "clean" a verbose regex back to a bare pattern string, so any patterns that are passed to HTML pattern can't be verbose. See https://stackoverflow.com/q/35640871/1354930

@azmeuk

azmeuk commented May 8, 2026

Copy link
Copy Markdown
Member

I added a html_parameter param to the regex validator.

  • If False, the default, no html pattern is added.
  • If True, the python pattern is used
  • If str, a custom pattern is used
  • This can also be a callable that produce a str or a bool, if people want to try applying conversion methods.

@azmeuk
azmeuk merged commit e4025ce into pallets-eco:main May 8, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature, or existing feature improvement

Development

Successfully merging this pull request may close these issues.

4 participants