diff options
| author | Si Feng <si.feng@me.com> | 2013-07-21 17:27:56 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-02-10 05:38:43 -0500 |
| commit | b102c27ff4d21ea6262e600227530f75337a5df2 (patch) | |
| tree | 448783436b3dd1bcc27576d9cc1d7e5fbc3477a8 /django | |
| parent | 0d98422b1365ae1e75051036936aab31248d5ee1 (diff) | |
Fixed #20784 -- Added inverse_match parameter to RegexValidator.
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/validators.py | 11 |
1 files changed, 8 insertions, 3 deletions
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): |
