Fixed #14184 -- Enabled running the validators in MultiValueFields. Thanks, paulcollins.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17430 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2012-02-04 12:48:12 +00:00
parent 954e3b4ad3
commit 9c045d00dc
2 changed files with 30 additions and 0 deletions

View File

@ -908,6 +908,7 @@ class MultiValueField(Field):
out = self.compress(clean_data)
self.validate(out)
self.run_validators(out)
return out
def compress(self, data_list):

View File

@ -1778,3 +1778,32 @@ class FormsTestCase(TestCase):
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'})