Ticket #7014: newforms-dates-1900.diff

File newforms-dates-1900.diff, 2.0 KB (added by Simon Litchfield <simon@…>, 17 years ago)

Patch to raise ValidationError instead of ValueError for dates before 1900

  • django/newforms/fields.py

     
    281281            return value
    282282        for format in self.input_formats:
    283283            try:
    284                 return datetime.date(*time.strptime(value, format)[:3])
     284                value = datetime.date(*time.strptime(value, format)[:3])
    285285            except ValueError:
    286286                continue
     287            if value.year < 1900:
     288                break
     289            return value
    287290        raise ValidationError(self.error_messages['invalid'])
    288291
    289292DEFAULT_TIME_INPUT_FORMATS = (
     
    359362            value = '%s %s' % tuple(value)
    360363        for format in self.input_formats:
    361364            try:
    362                 return datetime.datetime(*time.strptime(value, format)[:6])
     365                value = datetime.datetime(*time.strptime(value, format)[:6])
    363366            except ValueError:
    364367                continue
     368            if value.year < 1900:
     369                break
     370            return value
    365371        raise ValidationError(self.error_messages['invalid'])
    366372
    367373class RegexField(CharField):
  • tests/regressiontests/forms/fields.py

     
    442442Traceback (most recent call last):
    443443...
    444444ValidationError: [u'Enter a valid date.']
     445>>> f.clean('1/1/1801')
     446Traceback (most recent call last):
     447...
     448ValidationError: [u'Enter a valid date.']
    445449>>> f.clean(None)
    446450Traceback (most recent call last):
    447451...
     
    562566Traceback (most recent call last):
    563567...
    564568ValidationError: [u'Enter a valid date/time.']
     569>>> f.clean('10/10/1801 14:30')
     570Traceback (most recent call last):
     571...
     572ValidationError: [u'Enter a valid date/time.']
    565573
    566574DateField accepts an optional input_formats parameter:
    567575>>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p'])
Back to Top