Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit b56ef75088e17fa3555766e92a6747411ccd738c 1 parent db2f9bf
Chris Beaven authored June 09, 2011
3  django/core/validators.py
@@ -29,8 +29,9 @@ def __init__(self, regex=None, message=None, code=None):
29 29
         if code is not None:
30 30
             self.code = code
31 31
 
  32
+        # Compile the regex if it was not passed pre-compiled.
32 33
         if isinstance(self.regex, basestring):
33  
-            self.regex = re.compile(regex)
  34
+            self.regex = re.compile(self.regex)
34 35
 
35 36
     def __call__(self, value):
36 37
         """
34  docs/ref/validators.txt
@@ -60,26 +60,31 @@ to, or in lieu of custom ``field.clean()`` methods.
60 60
 
61 61
 ``RegexValidator``
62 62
 ------------------
63  
-.. class:: RegexValidator(regex, [message=None, code=None])
  63
+.. class:: RegexValidator([regex=None, message=None, code=None])
  64
+
  65
+    :param regex: If not ``None``, overrides :attr:`regex`. Can be a regular
  66
+        expression string or a pre-compiled regular expression.
  67
+    :param message: If not ``None``, overrides :attr:`.message`.
  68
+    :param code: If not ``None``, overrides :attr:`code`.
64 69
 
65 70
     .. attribute:: regex
66 71
 
67 72
         The regular expression pattern to search for the provided ``value``,
68 73
         or a pre-compiled regular expression. Raises a
69  
-        :exc:`~django.core.exceptions.ValidationError` with :attr:`.message`
70  
-        and :attr:`.code` if no match is found.
  74
+        :exc:`~django.core.exceptions.ValidationError` with :attr:`message`
  75
+        and :attr:`code` if no match is found. By default, matches any string
  76
+        (including an empty string).
71 77
 
72 78
     .. attribute:: message
73 79
 
74  
-        The error message used by :exc:`~django.core.exceptions.ValidationError`
75  
-        if validation fails. If no :attr:`.message` is specified, a generic
76  
-        ``"Enter a valid value"`` message is used. Default value: ``None``.
  80
+        The error message used by
  81
+        :exc:`~django.core.exceptions.ValidationError` if validation fails.
  82
+        Defaults to ``"Enter a valid value"``.
77 83
 
78 84
     .. attribute:: code
79 85
 
80 86
         The error code used by :exc:`~django.core.exceptions.ValidationError`
81  
-        if validation fails. If :attr:`.code` is not specified, ``"invalid"``
82  
-        is used. Default value: ``None``.
  87
+        if validation fails. Defaults to ``"invalid"``.
83 88
 
84 89
 ``URLValidator``
85 90
 ----------------
@@ -90,16 +95,19 @@ to, or in lieu of custom ``field.clean()`` methods.
90 95
     404 status code). Raises an error code of ``'invalid'`` if it doesn't look
91 96
     like a URL, and a code of ``'invalid_link'`` if it doesn't exist.
92 97
 
  98
+    :param verify_exists: Sets :attr:`verify_exists`. Defaults to ``False``.
  99
+    :param validator_user_agent: Sets :attr:`validator_user_agent`. Defaults to
  100
+        :setting:`URL_VALIDATOR_USER_AGENT` or, if that setting is set to a
  101
+        null value, ``"Django (http://www.djangoproject.com/)"``.
  102
+
93 103
     .. attribute:: verify_exists
94 104
 
95  
-        Default value: ``False``. If set to ``True``, this validator checks
96  
-        that the URL actually exists.
  105
+        If set to ``True``, this validator checks that the URL actually exists.
97 106
 
98 107
     .. attribute:: validator_user_agent
99 108
 
100  
-        If :attr:`.verify_exists` is ``True``, Django uses the value of
101  
-        :attr:`.validator_user_agent` as the "User-agent" for the request. This
102  
-        defaults to :setting:`settings.URL_VALIDATOR_USER_AGENT <URL_VALIDATOR_USER_AGENT>`.
  109
+        If :attr:`verify_exists` is ``True``, Django uses this value as the
  110
+        "User-agent" for the request.
103 111
 
104 112
 ``validate_email``
105 113
 ------------------
5  tests/modeltests/validators/tests.py
@@ -112,6 +112,11 @@
112 112
     (BaseValidator(True), True, None),
113 113
     (BaseValidator(True), False, ValidationError),
114 114
 
  115
+    (RegexValidator(), '', None),
  116
+    (RegexValidator(), 'x1x2', None),
  117
+    (RegexValidator('[0-9]+'), 'xxxxxx', ValidationError),
  118
+    (RegexValidator('[0-9]+'), '1234', None),
  119
+    (RegexValidator(re.compile('[0-9]+')), '1234', None),
115 120
     (RegexValidator('.*'), '', None),
116 121
     (RegexValidator(re.compile('.*')), '', None),
117 122
     (RegexValidator('.*'), 'xxxxx', None),

0 notes on commit b56ef75

Please sign in to comment.
Something went wrong with that request. Please try again.