diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index c78347672a38..446c6b37e3ef 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,6 +1,6 @@ #### Trac ticket number - + ticket-XXXXX diff --git a/AUTHORS b/AUTHORS index 9379176a4c0d..bea9d3fd4de5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -35,6 +35,7 @@ answer newbie questions, and generally made Django that much better: Ahmad Al-Ibrahim Ahmed Eltawela Ahmed Nassar + Aina Anjary Fenomamy R. ajs Akash Agrawal Akash Kumar Sen diff --git a/django/forms/fields.py b/django/forms/fields.py index 6408be2f060c..7f978aa03d62 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -965,7 +965,8 @@ def validate(self, value): if self.required and not value: raise ValidationError(self.error_messages["required"], code="required") # Validate that each value in the value list is in self.choices. - for val in value: + # Use set() to avoid redundant validation. + for val in set(value): if not self.valid_value(val): raise ValidationError( self.error_messages["invalid_choice"], diff --git a/tests/forms_tests/field_tests/test_multiplechoicefield.py b/tests/forms_tests/field_tests/test_multiplechoicefield.py index f316e1de8ed5..90a47ceee021 100644 --- a/tests/forms_tests/field_tests/test_multiplechoicefield.py +++ b/tests/forms_tests/field_tests/test_multiplechoicefield.py @@ -76,3 +76,20 @@ def test_multiplechoicefield_changed(self): def test_disabled_has_changed(self): f = MultipleChoiceField(choices=[("1", "One"), ("2", "Two")], disabled=True) self.assertIs(f.has_changed("x", "y"), False) + + def test_validate_duplicated_values(self): + f = MultipleChoiceField( + choices=[ + ("1", "One"), + ("2", "Two"), + ("3", "Three"), + ("4", "Four"), + ("5", "Five"), + ] + ) + self.assertIsNone(f.validate(["4", "4", "5", "5"])) + + def test_validate_duplicated_invalid_values(self): + f = MultipleChoiceField(choices=[("1", "one"), ("2", "Two")]) + with self.assertRaises(ValidationError): + f.validate(["1", "1", "invalid", "invalid"])