Ticket #14144: 14144.diff

File 14144.diff, 1.6 KB (added by matiasb, 14 years ago)

Patch and tests for ModelMultipleChoiceField clean method to run validators

  • django/forms/models.py

     
    10471047        for val in value:
    10481048            if force_unicode(val) not in pks:
    10491049                raise ValidationError(self.error_messages['invalid_choice'] % val)
     1050        # Since this override inherited ModelChoiceField clean implementation,
     1051        # we run custom validators here
     1052        self.run_validators(value)
    10501053        return qs
    10511054
    10521055    def prepare_value(self, value):
  • tests/regressiontests/model_forms_regress/tests.py

     
    2323        f = forms.ModelMultipleChoiceField(queryset=Person.objects.all())
    2424        self.assertNumQueries(1, f.clean, [1, 3, 5, 7, 9])
    2525
     26    def test_model_multiple_choice_run_validators(self):
     27        """
     28        Test that ModelMultipleChoiceField run given validators (#14144).
     29        """
     30        for i in range(30):
     31            Person.objects.create(name="Person %s" % i)
     32
     33        self._validator_run = False
     34        def my_validator(value):
     35            self._validator_run = True
     36       
     37        f = forms.ModelMultipleChoiceField(queryset=Person.objects.all(),
     38                                           validators=[my_validator])
     39        f.clean([1,2])
     40        self.assertTrue(self._validator_run)
     41
    2642class TripleForm(forms.ModelForm):
    2743    class Meta:
    2844        model = Triple
Back to Top