Ticket #4630: splitdatetimefield.patch
File splitdatetimefield.patch, 2.6 KB (added by , 17 years ago) |
---|
-
django/newforms/fields.py
516 516 """ 517 517 clean_data = [] 518 518 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: 524 526 raise ValidationError(gettext(u'Enter a list of values.')) 525 527 for i, field in enumerate(self.fields): 526 528 try: … … 558 560 559 561 def compress(self, data_list): 560 562 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.')) 561 569 return datetime.datetime.combine(*data_list) 562 570 return None -
tests/regressiontests/forms/tests.py
1859 1859 >>> f = SplitDateTimeField(required=False) 1860 1860 >>> f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)]) 1861 1861 datetime.datetime(2006, 1, 10, 7, 30) 1862 >>> f.clean(['2006-01-10', '07:30']) 1863 datetime.datetime(2006, 1, 10, 7, 30) 1862 1864 >>> f.clean(None) 1863 1865 >>> f.clean('') 1866 >>> f.clean(['']) 1867 >>> f.clean(['', '']) 1864 1868 >>> f.clean('hello') 1865 1869 Traceback (most recent call last): 1866 1870 ... … … 1877 1881 Traceback (most recent call last): 1878 1882 ... 1879 1883 ValidationError: [u'Enter a valid date.'] 1884 >>> f.clean(['2006-01-10', '']) 1885 Traceback (most recent call last): 1886 ... 1887 ValidationError: [u'Enter a valid time.'] 1888 >>> f.clean(['2006-01-10']) 1889 Traceback (most recent call last): 1890 ... 1891 ValidationError: [u'Enter a valid time.'] 1892 >>> f.clean(['', '07:30']) 1893 Traceback (most recent call last): 1894 ... 1895 ValidationError: [u'Enter a valid date.'] 1880 1896 1881 1897 ######### 1882 1898 # Forms #