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

File 0001-Added-reject-kwarg.diff, 1.8 KB (added by Antti Rasinen, 16 years ago)

Adds reject-parameter to RegexField

  • django/forms/fields.py

    From d9e23773aca210033bb4075ccbcdf2c54e25bac7 Mon Sep 17 00:00:00 2001
    From: Antti Rasinen <ars@iki.fi>
    Date: Fri, 23 Jan 2009 22:33:17 +0200
    Subject: [PATCH] Added reject-kwarg
    
    ---
     django/forms/fields.py |   12 ++++++++++--
     1 files changed, 10 insertions(+), 2 deletions(-)
    
    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index ccb54d8..e157ac0 100644
    a b class DateTimeField(Field):  
    390390        raise ValidationError(self.error_messages['invalid'])
    391391
    392392class RegexField(CharField):
    393     def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs):
     393    default_error_messages = {
     394        'reject': _(u'Enter a valid value'),
     395    }
     396   
     397    def __init__(self, regex, reject=None, max_length=None, min_length=None, error_message=None, *args, **kwargs):
    394398        """
    395399        regex can be either a string or a compiled regular expression object.
    396400        error_message is an optional error message to use, if
    class RegexField(CharField):  
    404408        super(RegexField, self).__init__(max_length, min_length, *args, **kwargs)
    405409        if isinstance(regex, basestring):
    406410            regex = re.compile(regex)
    407         self.regex = regex
     411        if reject and isinstance(reject, basestring):
     412            reject = re.compile(reject)
     413        self.regex, self.reject = regex, reject
    408414
    409415    def clean(self, value):
    410416        """
    class RegexField(CharField):  
    416422            return value
    417423        if not self.regex.search(value):
    418424            raise ValidationError(self.error_messages['invalid'])
     425        if self.reject and self.reject.search(value):
     426            raise ValidationError(self.error_messages['rejected'])
    419427        return value
    420428
    421429email_re = re.compile(
Back to Top