Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4630 -- Fixed some validation problems with SplitDateTimeField…

…. Thanks

glin@seznam.cz and SmileyChris.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5515 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit dfea6bdfa5f629679960115f26498fb4a378d75b 1 parent 08aa5c5
Malcolm Tredinnick authored June 23, 2007
18  django/newforms/fields.py
@@ -516,11 +516,13 @@ def clean(self, value):
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 [v for v in value if v 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,5 +560,11 @@ def __init__(self, *args, **kwargs):
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
16  tests/regressiontests/forms/tests.py
@@ -1859,8 +1859,12 @@
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,6 +1881,18 @@
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 #

0 notes on commit dfea6bd

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