Fixes #13511 -- make regex parameter to RegexValidator to be optional. Also tidies up related docs (parameters aren't attributes). Thanks for the patch work, davidfischer.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16351 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
db2f9bfae1
commit
b56ef75088
|
@ -29,8 +29,9 @@ class RegexValidator(object):
|
||||||
if code is not None:
|
if code is not None:
|
||||||
self.code = code
|
self.code = code
|
||||||
|
|
||||||
|
# Compile the regex if it was not passed pre-compiled.
|
||||||
if isinstance(self.regex, basestring):
|
if isinstance(self.regex, basestring):
|
||||||
self.regex = re.compile(regex)
|
self.regex = re.compile(self.regex)
|
||||||
|
|
||||||
def __call__(self, value):
|
def __call__(self, value):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -60,26 +60,31 @@ to, or in lieu of custom ``field.clean()`` methods.
|
||||||
|
|
||||||
``RegexValidator``
|
``RegexValidator``
|
||||||
------------------
|
------------------
|
||||||
.. class:: RegexValidator(regex, [message=None, code=None])
|
.. class:: RegexValidator([regex=None, message=None, code=None])
|
||||||
|
|
||||||
|
:param regex: If not ``None``, overrides :attr:`regex`. Can be a regular
|
||||||
|
expression string or a pre-compiled regular expression.
|
||||||
|
:param message: If not ``None``, overrides :attr:`.message`.
|
||||||
|
:param code: If not ``None``, overrides :attr:`code`.
|
||||||
|
|
||||||
.. 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.
|
and :attr:`code` if no match is found. By default, matches any string
|
||||||
|
(including an empty string).
|
||||||
|
|
||||||
.. attribute:: message
|
.. attribute:: message
|
||||||
|
|
||||||
The error message used by :exc:`~django.core.exceptions.ValidationError`
|
The error message used by
|
||||||
if validation fails. If no :attr:`.message` is specified, a generic
|
:exc:`~django.core.exceptions.ValidationError` if validation fails.
|
||||||
``"Enter a valid value"`` message is used. Default value: ``None``.
|
Defaults to ``"Enter a valid value"``.
|
||||||
|
|
||||||
.. attribute:: code
|
.. attribute:: code
|
||||||
|
|
||||||
The error code used by :exc:`~django.core.exceptions.ValidationError`
|
The error code used by :exc:`~django.core.exceptions.ValidationError`
|
||||||
if validation fails. If :attr:`.code` is not specified, ``"invalid"``
|
if validation fails. Defaults to ``"invalid"``.
|
||||||
is used. Default value: ``None``.
|
|
||||||
|
|
||||||
``URLValidator``
|
``URLValidator``
|
||||||
----------------
|
----------------
|
||||||
|
@ -90,16 +95,19 @@ to, or in lieu of custom ``field.clean()`` methods.
|
||||||
404 status code). Raises an error code of ``'invalid'`` if it doesn't look
|
404 status code). Raises an error code of ``'invalid'`` if it doesn't look
|
||||||
like a URL, and a code of ``'invalid_link'`` if it doesn't exist.
|
like a URL, and a code of ``'invalid_link'`` if it doesn't exist.
|
||||||
|
|
||||||
|
:param verify_exists: Sets :attr:`verify_exists`. Defaults to ``False``.
|
||||||
|
:param validator_user_agent: Sets :attr:`validator_user_agent`. Defaults to
|
||||||
|
:setting:`URL_VALIDATOR_USER_AGENT` or, if that setting is set to a
|
||||||
|
null value, ``"Django (http://www.djangoproject.com/)"``.
|
||||||
|
|
||||||
.. attribute:: verify_exists
|
.. attribute:: verify_exists
|
||||||
|
|
||||||
Default value: ``False``. If set to ``True``, this validator checks
|
If set to ``True``, this validator checks that the URL actually exists.
|
||||||
that the URL actually exists.
|
|
||||||
|
|
||||||
.. attribute:: validator_user_agent
|
.. attribute:: validator_user_agent
|
||||||
|
|
||||||
If :attr:`.verify_exists` is ``True``, Django uses the value of
|
If :attr:`verify_exists` is ``True``, Django uses this value as the
|
||||||
:attr:`.validator_user_agent` as the "User-agent" for the request. This
|
"User-agent" for the request.
|
||||||
defaults to :setting:`settings.URL_VALIDATOR_USER_AGENT <URL_VALIDATOR_USER_AGENT>`.
|
|
||||||
|
|
||||||
``validate_email``
|
``validate_email``
|
||||||
------------------
|
------------------
|
||||||
|
|
|
@ -112,6 +112,11 @@ TEST_DATA = (
|
||||||
(BaseValidator(True), True, None),
|
(BaseValidator(True), True, None),
|
||||||
(BaseValidator(True), False, ValidationError),
|
(BaseValidator(True), False, ValidationError),
|
||||||
|
|
||||||
|
(RegexValidator(), '', None),
|
||||||
|
(RegexValidator(), 'x1x2', None),
|
||||||
|
(RegexValidator('[0-9]+'), 'xxxxxx', ValidationError),
|
||||||
|
(RegexValidator('[0-9]+'), '1234', None),
|
||||||
|
(RegexValidator(re.compile('[0-9]+')), '1234', None),
|
||||||
(RegexValidator('.*'), '', None),
|
(RegexValidator('.*'), '', None),
|
||||||
(RegexValidator(re.compile('.*')), '', None),
|
(RegexValidator(re.compile('.*')), '', None),
|
||||||
(RegexValidator('.*'), 'xxxxx', None),
|
(RegexValidator('.*'), 'xxxxx', None),
|
||||||
|
|
Loading…
Reference in New Issue