Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@malcolmt malcolmt authored
Showing with 29 additions and 5 deletions.
  1. +13 −5 django/newforms/fields.py
  2. +16 −0 tests/regressiontests/forms/tests.py
View
18 django/newforms/fields.py
@@ -516,11 +516,13 @@ def clean(self, value):
"""
clean_data = []
errors = ErrorList()
- if self.required and not value:
- raise ValidationError(gettext(u'This field is required.'))
- elif not self.required and not value:
- return self.compress([])
- if not isinstance(value, (list, tuple)):
+ if not value or isinstance(value, (list, tuple)):
+ if not value or not [v for v in value if v not in EMPTY_VALUES]:
+ if self.required:
+ raise ValidationError(gettext(u'This field is required.'))
+ else:
+ return self.compress([])
+ else:
raise ValidationError(gettext(u'Enter a list of values.'))
for i, field in enumerate(self.fields):
try:
@@ -558,5 +560,11 @@ def __init__(self, *args, **kwargs):
def compress(self, data_list):
if data_list:
+ # Raise a validation error if time or date is empty
+ # (possible if SplitDateTimeField has required=False).
+ if data_list[0] in EMPTY_VALUES:
+ raise ValidationError(gettext(u'Enter a valid date.'))
+ if data_list[1] in EMPTY_VALUES:
+ raise ValidationError(gettext(u'Enter a valid time.'))
return datetime.datetime.combine(*data_list)
return None
View
16 tests/regressiontests/forms/tests.py
@@ -1859,8 +1859,12 @@
>>> f = SplitDateTimeField(required=False)
>>> f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])
datetime.datetime(2006, 1, 10, 7, 30)
+>>> f.clean(['2006-01-10', '07:30'])
+datetime.datetime(2006, 1, 10, 7, 30)
>>> f.clean(None)
>>> f.clean('')
+>>> f.clean([''])
+>>> f.clean(['', ''])
>>> f.clean('hello')
Traceback (most recent call last):
...
@@ -1877,6 +1881,18 @@
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid date.']
+>>> f.clean(['2006-01-10', ''])
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid time.']
+>>> f.clean(['2006-01-10'])
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid time.']
+>>> f.clean(['', '07:30'])
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid date.']
#########
# Forms #
Please sign in to comment.
Something went wrong with that request. Please try again.