diff --git a/django/forms/models.py b/django/forms/models.py index 4ffc3b6fac1..de52b6a7e0f 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -1045,6 +1045,9 @@ class ModelMultipleChoiceField(ModelChoiceField): for val in value: if force_unicode(val) not in pks: 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 def prepare_value(self, value): diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py index e38df4a80f6..2e2afd6c05e 100644 --- a/tests/regressiontests/model_forms_regress/tests.py +++ b/tests/regressiontests/model_forms_regress/tests.py @@ -24,6 +24,22 @@ class ModelMultipleChoiceFieldTests(TestCase): f = forms.ModelMultipleChoiceField(queryset=Person.objects.all()) 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 Meta: model = Triple