Ticket #13511: 13511-regexvalidator2.diff

File 13511-regexvalidator2.diff, 2.0 KB (added by David Fischer, 13 years ago)

Patch leaves "regex" as optional, but documents and tests functionality

  • docs/ref/validators.txt

     
    6060
    6161``RegexValidator``
    6262------------------
    63 .. class:: RegexValidator(regex, [message=None, code=None])
     63.. class:: RegexValidator([regex=None, message=None, code=None])
    6464
    6565    .. attribute:: regex
    6666
    6767        The regular expression pattern to search for the provided ``value``,
    6868        or a pre-compiled regular expression. Raises a
    6969        :exc:`~django.core.exceptions.ValidationError` with :attr:`.message`
    70         and :attr:`.code` if no match is found.
     70        and :attr:`.code` if no match is found. Matches any string (including
     71        the empty string) if set to ``None``.
    7172
    7273    .. attribute:: message
    7374
  • tests/modeltests/validators/tests.py

     
    112112    (BaseValidator(True), True, None),
    113113    (BaseValidator(True), False, ValidationError),
    114114
     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),
    115120    (RegexValidator('.*'), '', None),
    116121    (RegexValidator(re.compile('.*')), '', None),
    117122    (RegexValidator('.*'), 'xxxxx', None),
  • django/core/validators.py

     
    2929        if code is not None:
    3030            self.code = code
    3131
     32        # compile the regex if it was not passed pre-compiled
    3233        if isinstance(self.regex, basestring):
    33             self.regex = re.compile(regex)
     34            self.regex = re.compile(self.regex)
    3435
    3536    def __call__(self, value):
    3637        """
Back to Top