Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #14184 -- Enabled running the validators in MultiValueFields. T…

…hanks, paulcollins.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17430 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9c045d00dc37f4eb082e022ac12b659345413d7e 1 parent 954e3b4
Jannis Leidel jezdez authored
1  django/forms/fields.py
View
@@ -908,6 +908,7 @@ def clean(self, value):
out = self.compress(clean_data)
self.validate(out)
+ self.run_validators(out)
return out
def compress(self, data_list):
29 tests/regressiontests/forms/tests/forms.py
View
@@ -1778,3 +1778,32 @@ class EventForm(Form):
form = EventForm()
self.assertHTMLEqual(form.as_ul(), u'<input type="hidden" name="happened_at_0" id="id_happened_at_0" /><input type="hidden" name="happened_at_1" id="id_happened_at_1" />')
+
+ def test_multivalue_field_validation(self):
+ def bad_names(value):
+ if value == 'bad value':
+ raise ValidationError('bad value not allowed')
+
+ class NameField(MultiValueField):
+ def __init__(self, fields=(), *args, **kwargs):
+ fields = (CharField(label='First name', max_length=10),
+ CharField(label='Last name', max_length=10))
+ super(NameField, self).__init__(fields=fields, *args, **kwargs)
+
+ def compress(self, data_list):
+ return ' '.join(data_list)
+
+ class NameForm(Form):
+ name = NameField(validators=[bad_names])
+
+ form = NameForm(data={'name' : ['bad', 'value']})
+ form.full_clean()
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form.errors, {'name': [u'bad value not allowed']})
+ form = NameForm(data={'name' : ['should be overly', 'long for the field names']})
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form.errors, {'name': [u'Ensure this value has at most 10 characters (it has 16).',
+ u'Ensure this value has at most 10 characters (it has 24).']})
+ form = NameForm(data={'name' : ['fname', 'lname']})
+ self.assertTrue(form.is_valid())
+ self.assertEqual(form.cleaned_data, {'name' : 'fname lname'})
Please sign in to comment.
Something went wrong with that request. Please try again.