Fixed #20784 -- Added inverse_match parameter to RegexValidator.
This commit is contained in:
parent
0d98422b13
commit
b102c27ff4
|
@ -20,14 +20,17 @@ class RegexValidator(object):
|
||||||
regex = ''
|
regex = ''
|
||||||
message = _('Enter a valid value.')
|
message = _('Enter a valid value.')
|
||||||
code = 'invalid'
|
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:
|
if regex is not None:
|
||||||
self.regex = regex
|
self.regex = regex
|
||||||
if message is not None:
|
if message is not None:
|
||||||
self.message = message
|
self.message = message
|
||||||
if code is not None:
|
if code is not None:
|
||||||
self.code = code
|
self.code = code
|
||||||
|
if inverse_match is not None:
|
||||||
|
self.inverse_match = inverse_match
|
||||||
|
|
||||||
# Compile the regex if it was not passed pre-compiled.
|
# Compile the regex if it was not passed pre-compiled.
|
||||||
if isinstance(self.regex, six.string_types):
|
if isinstance(self.regex, six.string_types):
|
||||||
|
@ -35,9 +38,11 @@ class RegexValidator(object):
|
||||||
|
|
||||||
def __call__(self, value):
|
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)
|
raise ValidationError(self.message, code=self.code)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
|
|
@ -59,20 +59,22 @@ to, or in lieu of custom ``field.clean()`` methods.
|
||||||
|
|
||||||
``RegexValidator``
|
``RegexValidator``
|
||||||
------------------
|
------------------
|
||||||
.. class:: RegexValidator([regex=None, message=None, code=None])
|
.. class:: RegexValidator([regex=None, message=None, code=None, inverse_match=None])
|
||||||
|
|
||||||
:param regex: If not ``None``, overrides :attr:`regex`. Can be a regular
|
:param regex: If not ``None``, overrides :attr:`regex`. Can be a regular
|
||||||
expression string or a pre-compiled regular expression.
|
expression string or a pre-compiled regular expression.
|
||||||
:param message: If not ``None``, overrides :attr:`.message`.
|
:param message: If not ``None``, overrides :attr:`.message`.
|
||||||
:param code: If not ``None``, overrides :attr:`code`.
|
:param code: If not ``None``, overrides :attr:`code`.
|
||||||
|
:param inverse_match: If not ``None``, overrides :attr:`inverse_match`.
|
||||||
|
|
||||||
.. attribute:: regex
|
.. attribute:: regex
|
||||||
|
|
||||||
The regular expression pattern to search for the provided ``value``,
|
The regular expression pattern to search for the provided ``value``,
|
||||||
or a pre-compiled regular expression. Raises a
|
or a pre-compiled regular expression. Raises a
|
||||||
:exc:`~django.core.exceptions.ValidationError` with :attr:`message`
|
:exc:`~django.core.exceptions.ValidationError` with :attr:`message`
|
||||||
and :attr:`code` if no match is found. By default, matches any string
|
and :attr:`code` if :attr:`inverse_match` is ``False`` and a match is
|
||||||
(including an empty string).
|
found, or if :attr:`inverse_match` is ``True`` and a match is not found.
|
||||||
|
By default, matches any string (including an empty string).
|
||||||
|
|
||||||
.. attribute:: message
|
.. attribute:: message
|
||||||
|
|
||||||
|
@ -85,6 +87,12 @@ to, or in lieu of custom ``field.clean()`` methods.
|
||||||
The error code used by :exc:`~django.core.exceptions.ValidationError`
|
The error code used by :exc:`~django.core.exceptions.ValidationError`
|
||||||
if validation fails. Defaults to ``"invalid"``.
|
if validation fails. Defaults to ``"invalid"``.
|
||||||
|
|
||||||
|
.. attribute:: inverse_match
|
||||||
|
|
||||||
|
.. versionadded:: 1.7
|
||||||
|
|
||||||
|
The match mode for :attr:`regex`. Defaults to ``False``.
|
||||||
|
|
||||||
``URLValidator``
|
``URLValidator``
|
||||||
----------------
|
----------------
|
||||||
.. class:: URLValidator([schemes=None, regex=None, message=None, code=None])
|
.. class:: URLValidator([schemes=None, regex=None, message=None, code=None])
|
||||||
|
|
|
@ -697,6 +697,12 @@ Tests
|
||||||
Validators
|
Validators
|
||||||
^^^^^^^^^^
|
^^^^^^^^^^
|
||||||
|
|
||||||
|
* :class:`~django.core.validators.RegexValidator` now accepts an optional
|
||||||
|
Boolean :attr:`~django.core.validators.RegexValidator.inverse_match` argument
|
||||||
|
which determines if the :exc:`~django.core.exceptions.ValidationError` should
|
||||||
|
be raised when the regular expression pattern matches (``True``) or does not
|
||||||
|
match (``False``, by default) the provided ``value``.
|
||||||
|
|
||||||
* :class:`~django.core.validators.URLValidator` now accepts an optional
|
* :class:`~django.core.validators.URLValidator` now accepts an optional
|
||||||
``schemes`` argument which allows customization of the accepted URI schemes
|
``schemes`` argument which allows customization of the accepted URI schemes
|
||||||
(instead of the defaults ``http(s)`` and ``ftp(s)``).
|
(instead of the defaults ``http(s)`` and ``ftp(s)``).
|
||||||
|
|
|
@ -187,6 +187,10 @@ TEST_DATA = (
|
||||||
|
|
||||||
(RegexValidator('x'), 'y', ValidationError),
|
(RegexValidator('x'), 'y', ValidationError),
|
||||||
(RegexValidator(re.compile('x')), 'y', ValidationError),
|
(RegexValidator(re.compile('x')), 'y', ValidationError),
|
||||||
|
(RegexValidator('x', inverse_match=True), 'y', None),
|
||||||
|
(RegexValidator(re.compile('x'), inverse_match=True), 'y', None),
|
||||||
|
(RegexValidator('x', inverse_match=True), 'x', ValidationError),
|
||||||
|
(RegexValidator(re.compile('x'), inverse_match=True), 'x', ValidationError),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue