Fixed #14144 -- Made sure custom validators are called in ModelMultipleChoiceFields. Thanks, matiasb.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14886 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-12-12 22:57:37 +00:00
parent dad28e8557
commit 34daa0804c
2 changed files with 19 additions and 0 deletions

View File

@ -1045,6 +1045,9 @@ class ModelMultipleChoiceField(ModelChoiceField):
for val in value: for val in value:
if force_unicode(val) not in pks: if force_unicode(val) not in pks:
raise ValidationError(self.error_messages['invalid_choice'] % val) raise ValidationError(self.error_messages['invalid_choice'] % val)
# Since this overrides the inherited ModelChoiceField.clean
# we run custom validators here
self.run_validators(value)
return qs return qs
def prepare_value(self, value): def prepare_value(self, value):

View File

@ -24,6 +24,22 @@ class ModelMultipleChoiceFieldTests(TestCase):
f = forms.ModelMultipleChoiceField(queryset=Person.objects.all()) f = forms.ModelMultipleChoiceField(queryset=Person.objects.all())
self.assertNumQueries(1, f.clean, [1, 3, 5, 7, 9]) self.assertNumQueries(1, f.clean, [1, 3, 5, 7, 9])
def test_model_multiple_choice_run_validators(self):
"""
Test that ModelMultipleChoiceField run given validators (#14144).
"""
for i in range(30):
Person.objects.create(name="Person %s" % i)
self._validator_run = False
def my_validator(value):
self._validator_run = True
f = forms.ModelMultipleChoiceField(queryset=Person.objects.all(),
validators=[my_validator])
f.clean([1,2])
self.assertTrue(self._validator_run)
class TripleForm(forms.ModelForm): class TripleForm(forms.ModelForm):
class Meta: class Meta:
model = Triple model = Triple