Opened 16 years ago

Closed 14 years ago

#5709 closed (wontfix)

modify forms.RegexField to support inverse matching

Reported by: Nathan Hoover <nathan@…> 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 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@…> 16 years ago.
diff to patch fields.py
fields.py.2.diff (1.2 KB ) - added by Nathan Hoover 16 years ago.
patch
0001-Added-reject-kwarg.diff (1.8 KB ) - added by Antti Rasinen 15 years ago.
Adds reject-parameter to RegexField
0001-Added-reject-kwarg.patch (1.8 KB ) - added by Antti Rasinen 15 years ago.
Adds reject-kwarg

Download all attachments as: .zip

Change History (12)

by Nathan Hoover <nathan@…>, 16 years ago

Attachment: fields.py.diff added

diff to patch fields.py

comment:1 by Nathan Hoover <nathan@…>, 16 years ago

Owner: changed from nobody to anonymous
Status: newassigned

comment:2 by Nathan Hoover, 16 years ago

Owner: changed from anonymous to Nathan Hoover
Status: assignednew

comment:3 by Malcolm Tredinnick, 16 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:

  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).

by Nathan Hoover, 16 years ago

Attachment: fields.py.2.diff added

patch

comment:4 by Nathan Hoover, 16 years ago

Thanks for the tips; still a python n00b here.

I changed the patch to reflect your observations.

comment:5 by Chris Beaven, 16 years ago

Triage Stage: UnreviewedDesign decision needed

comment:6 by Antti Rasinen, 15 years ago

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.

by Antti Rasinen, 15 years ago

Adds reject-parameter to RegexField

by Antti Rasinen, 15 years ago

Adds reject-kwarg

comment:7 by Antti Rasinen, 15 years ago

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

comment:8 by Russell Keith-Magee, 14 years ago

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