Opened 17 years ago
Closed 14 years ago
#5709 closed (wontfix)
modify forms.RegexField to support inverse matching
Reported by: | Owned by: | Nathan Hoover | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | RegexField | |
Cc: | Triage Stage: | Design decision needed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
For conveinence, it would be nice if the RegexField supported inverse matching; i.e. the contents of the field do NOT match the regex when they're valid. I needed this in the course of using a RegexField for username signups. I have a list of undesirable usernames, like so:
INVALID_USERNAME_REGEX = '(admin|root|add|edit|administrator|service)'
Since it's not trival to make a regex that uses ^
against words rather than characters this is useful.
Attachments (4)
Change History (12)
by , 17 years ago
Attachment: | fields.py.diff added |
---|
comment:1 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
comment:3 by , 17 years ago
Description: | modified (diff) |
---|
(Fixed wiki formatting in description).
I'm not sure at the moment whether this is something to add to RegexField or not, but a couple of immediate things about the patch:
- don't compare "== None". Instead use "is None"; it's slightly more idiomatic Python and a little more efficient.
- Any new parameters should go at the end of the argument list so as not to introduce backwards incompatible changes (your patch breaks any code using position arguments to RegexField).
comment:4 by , 17 years ago
Thanks for the tips; still a python n00b here.
I changed the patch to reflect your observations.
comment:5 by , 17 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:6 by , 16 years ago
Summary: | modify newforms.RegexField to support inverse matching → modify forms.RegexField to support inverse matching |
---|
I encountered a generalized version of this problem today. Consider a case, where valid data format is almost regular, but has a couple of inconsistencies. Often it is easier to make two regexes instead of one. First one filters out "good" stuff and the other one rejects the exceptions.
For example, the lottery numbers around here are from 1 to 39. Matching that with a single regex is a pain. But consider the following:
number = RegexField(r'[123]?\d', reject='(0|40)')
Or consider a badly planned urlconf after which we can't have login and logout as usernames:
username = RegexField(<username-re>, reject=r'(login|logout)')
Certain words just aren't fit for titles:
title = RegexField('', reject='(mammaries|copulation|solids)')
Although all of the above are possible to do with some regex-magic, I feel the use of the reject-parameter conveys the intention better than three lines of line noise.
I'll attach a patch which adds the reject-parameter.
by , 16 years ago
Attachment: | 0001-Added-reject-kwarg.diff added |
---|
Adds reject-parameter to RegexField
comment:7 by , 16 years ago
Added new patch, unfortunately has different file ending. It fixes a few bugs in the original patch.
comment:8 by , 14 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Now that we have custom validators back, this is something we can do without needing to introduce an API for a specific type of logical conjunction.
diff to patch fields.py