From b102c27ff4d21ea6262e600227530f75337a5df2 Mon Sep 17 00:00:00 2001 From: Si Feng Date: Sun, 21 Jul 2013 17:27:56 -0700 Subject: Fixed #20784 -- Added inverse_match parameter to RegexValidator. --- django/core/validators.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'django') diff --git a/django/core/validators.py b/django/core/validators.py index 6f4f570765..6bbee8e7d2 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -20,14 +20,17 @@ class RegexValidator(object): regex = '' message = _('Enter a valid value.') code = 'invalid' + inverse_match = False - def __init__(self, regex=None, message=None, code=None): + def __init__(self, regex=None, message=None, code=None, inverse_match=None): if regex is not None: self.regex = regex if message is not None: self.message = message if code is not None: self.code = code + if inverse_match is not None: + self.inverse_match = inverse_match # Compile the regex if it was not passed pre-compiled. if isinstance(self.regex, six.string_types): @@ -35,9 +38,11 @@ class RegexValidator(object): def __call__(self, value): """ - Validates that the input matches the regular expression. + Validates that the input matches the regular expression + if inverse_match is False, otherwise raises ValidationError. """ - if not self.regex.search(force_text(value)): + if not (self.inverse_match is not bool(self.regex.search( + force_text(value)))): raise ValidationError(self.message, code=self.code) def __eq__(self, other): -- cgit v1.3