Ticket #4630: splitdatetimefield.patch

File splitdatetimefield.patch, 2.6 KB (added by Chris Beaven, 17 years ago)
  • django/newforms/fields.py

     
    516516        """
    517517        clean_data = []
    518518        errors = ErrorList()
    519         if self.required and not value:
    520             raise ValidationError(gettext(u'This field is required.'))
    521         elif not self.required and not value:
    522             return self.compress([])
    523         if not isinstance(value, (list, tuple)):
     519        if not value or isinstance(value, (list, tuple)):
     520            if not value or not [field_value for field_value in value if field_value not in EMPTY_VALUES]:
     521                if self.required:
     522                    raise ValidationError(gettext(u'This field is required.'))
     523                else:
     524                    return self.compress([])
     525        else:
    524526            raise ValidationError(gettext(u'Enter a list of values.'))
    525527        for i, field in enumerate(self.fields):
    526528            try:
     
    558560
    559561    def compress(self, data_list):
    560562        if data_list:
     563            # Raise a validation error if time or date is empty
     564            # (possible if SplitDateTimeField has required=False).
     565            if data_list[0] in EMPTY_VALUES:
     566                raise ValidationError(gettext(u'Enter a valid date.'))
     567            if data_list[1] in EMPTY_VALUES:
     568                raise ValidationError(gettext(u'Enter a valid time.'))
    561569            return datetime.datetime.combine(*data_list)
    562570        return None
  • tests/regressiontests/forms/tests.py

     
    18591859>>> f = SplitDateTimeField(required=False)
    18601860>>> f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])
    18611861datetime.datetime(2006, 1, 10, 7, 30)
     1862>>> f.clean(['2006-01-10', '07:30'])
     1863datetime.datetime(2006, 1, 10, 7, 30)
    18621864>>> f.clean(None)
    18631865>>> f.clean('')
     1866>>> f.clean([''])
     1867>>> f.clean(['', ''])
    18641868>>> f.clean('hello')
    18651869Traceback (most recent call last):
    18661870...
     
    18771881Traceback (most recent call last):
    18781882...
    18791883ValidationError: [u'Enter a valid date.']
     1884>>> f.clean(['2006-01-10', ''])
     1885Traceback (most recent call last):
     1886...
     1887ValidationError: [u'Enter a valid time.']
     1888>>> f.clean(['2006-01-10'])
     1889Traceback (most recent call last):
     1890...
     1891ValidationError: [u'Enter a valid time.']
     1892>>> f.clean(['', '07:30'])
     1893Traceback (most recent call last):
     1894...
     1895ValidationError: [u'Enter a valid date.']
    18801896
    18811897#########
    18821898# Forms #
Back to Top