Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored February 04, 2012
1  django/forms/fields.py
@@ -908,6 +908,7 @@ def clean(self, value):
908 908
 
909 909
         out = self.compress(clean_data)
910 910
         self.validate(out)
  911
+        self.run_validators(out)
911 912
         return out
912 913
 
913 914
     def compress(self, data_list):
29  tests/regressiontests/forms/tests/forms.py
@@ -1778,3 +1778,32 @@ class EventForm(Form):
1778 1778
 
1779 1779
         form = EventForm()
1780 1780
         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" />')
  1781
+
  1782
+    def test_multivalue_field_validation(self):
  1783
+        def bad_names(value):
  1784
+            if value == 'bad value':
  1785
+                raise ValidationError('bad value not allowed')
  1786
+
  1787
+        class NameField(MultiValueField):
  1788
+            def __init__(self, fields=(), *args, **kwargs):
  1789
+                fields = (CharField(label='First name', max_length=10),
  1790
+                          CharField(label='Last name', max_length=10))
  1791
+                super(NameField, self).__init__(fields=fields, *args, **kwargs)
  1792
+
  1793
+            def compress(self, data_list):
  1794
+                return ' '.join(data_list)
  1795
+
  1796
+        class NameForm(Form):
  1797
+            name = NameField(validators=[bad_names])
  1798
+
  1799
+        form = NameForm(data={'name' : ['bad', 'value']})
  1800
+        form.full_clean()
  1801
+        self.assertFalse(form.is_valid())
  1802
+        self.assertEqual(form.errors, {'name': [u'bad value not allowed']})
  1803
+        form = NameForm(data={'name' : ['should be overly', 'long for the field names']})
  1804
+        self.assertFalse(form.is_valid())
  1805
+        self.assertEqual(form.errors, {'name': [u'Ensure this value has at most 10 characters (it has 16).',
  1806
+                                                u'Ensure this value has at most 10 characters (it has 24).']})
  1807
+        form = NameForm(data={'name' : ['fname', 'lname']})
  1808
+        self.assertTrue(form.is_valid())
  1809
+        self.assertEqual(form.cleaned_data, {'name' : 'fname lname'})

0 notes on commit 9c045d0

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