Code

Ticket #5709: 0001-Added-reject-kwarg.diff

File 0001-Added-reject-kwarg.diff, 1.8 KB (added by arsatiki, 5 years ago)

Adds reject-parameter to RegexField

Line 
1From d9e23773aca210033bb4075ccbcdf2c54e25bac7 Mon Sep 17 00:00:00 2001
2From: Antti Rasinen <ars@iki.fi>
3Date: Fri, 23 Jan 2009 22:33:17 +0200
4Subject: [PATCH] Added reject-kwarg
5
6---
7 django/forms/fields.py |   12 ++++++++++--
8 1 files changed, 10 insertions(+), 2 deletions(-)
9
10diff --git a/django/forms/fields.py b/django/forms/fields.py
11index ccb54d8..e157ac0 100644
12--- a/django/forms/fields.py
13+++ b/django/forms/fields.py
14@@ -390,7 +390,11 @@ class DateTimeField(Field):
15         raise ValidationError(self.error_messages['invalid'])
16 
17 class RegexField(CharField):
18-    def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs):
19+    default_error_messages = {
20+        'reject': _(u'Enter a valid value'),
21+    }
22+   
23+    def __init__(self, regex, reject=None, max_length=None, min_length=None, error_message=None, *args, **kwargs):
24         """
25         regex can be either a string or a compiled regular expression object.
26         error_message is an optional error message to use, if
27@@ -404,7 +408,9 @@ class RegexField(CharField):
28         super(RegexField, self).__init__(max_length, min_length, *args, **kwargs)
29         if isinstance(regex, basestring):
30             regex = re.compile(regex)
31-        self.regex = regex
32+        if reject and isinstance(reject, basestring):
33+            reject = re.compile(reject)
34+        self.regex, self.reject = regex, reject
35 
36     def clean(self, value):
37         """
38@@ -416,6 +422,8 @@ class RegexField(CharField):
39             return value
40         if not self.regex.search(value):
41             raise ValidationError(self.error_messages['invalid'])
42+        if self.reject and self.reject.search(value):
43+            raise ValidationError(self.error_messages['rejected'])
44         return value
45 
46 email_re = re.compile(
47--
481.6.1
49