Skip to content

Commit

Permalink
[1.2.X] Fixed #14144 -- Made sure custom validators are called in Mod…
Browse files Browse the repository at this point in the history
…elMultipleChoiceFields. Thanks, matiasb.

Backport from trunk (r14886).

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14906 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jezdez committed Dec 13, 2010
1 parent 69edfc7 commit 32bf35d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions django/forms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,9 @@ def clean(self, value):
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):
Expand Down
16 changes: 16 additions & 0 deletions tests/regressiontests/model_forms_regress/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ def test_model_multiple_choice_number_of_queries(self):
selected = f.clean([1, 3, 5, 7, 9])
self.assertEquals(len(db.connection.queries), 1)

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
Expand Down

0 comments on commit 32bf35d

Please sign in to comment.