Opened 9 years ago

Closed 6 years ago

#5709 closed (wontfix)

modify forms.RegexField to support inverse matching

Reported by: Nathan Hoover <nathan@…> Owned by: Nathan Hoover
Component: Forms Version: master
Severity: Keywords: RegexField
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Malcolm Tredinnick)

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)

fields.py.diff (1.2 KB) - added by Nathan Hoover <nathan@…> 9 years ago.
diff to patch fields.py
fields.py.2.diff (1.2 KB) - added by Nathan Hoover 9 years ago.
patch
0001-Added-reject-kwarg.diff (1.8 KB) - added by Antti Rasinen 8 years ago.
Adds reject-parameter to RegexField
0001-Added-reject-kwarg.patch (1.8 KB) - added by Antti Rasinen 8 years ago.
Adds reject-kwarg

Download all attachments as: .zip

Change History (12)

Changed 9 years ago by Nathan Hoover <nathan@…>

Attachment: fields.py.diff added

diff to patch fields.py

comment:1 Changed 9 years ago by Nathan Hoover <nathan@…>

Owner: changed from nobody to anonymous
Status: newassigned

comment:2 Changed 9 years ago by Nathan Hoover

Owner: changed from anonymous to Nathan Hoover
Status: assignednew

comment:3 Changed 9 years ago by Malcolm Tredinnick

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:

  1. don't compare "== None". Instead use "is None"; it's slightly more idiomatic Python and a little more efficient.
  2. 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).

Changed 9 years ago by Nathan Hoover

Attachment: fields.py.2.diff added

patch

comment:4 Changed 9 years ago by Nathan Hoover

Thanks for the tips; still a python n00b here.

I changed the patch to reflect your observations.

comment:5 Changed 9 years ago by Chris Beaven

Triage Stage: UnreviewedDesign decision needed

comment:6 Changed 8 years ago by Antti Rasinen

Summary: modify newforms.RegexField to support inverse matchingmodify 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.

Changed 8 years ago by Antti Rasinen

Adds reject-parameter to RegexField

Changed 8 years ago by Antti Rasinen

Adds reject-kwarg

comment:7 Changed 8 years ago by Antti Rasinen

Added new patch, unfortunately has different file ending. It fixes a few bugs in the original patch.

comment:8 Changed 6 years ago by Russell Keith-Magee

Resolution: wontfix
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top