Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#### Trac ticket number
<!-- Replace XXXXX with the corresponding Trac ticket number. -->
<!-- Or delete the line and write "N/A" if this is a trivial PR. -->
<!-- Or delete the line and write "N/A - typo" for typo fixes. -->

ticket-XXXXX

Expand Down
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ answer newbie questions, and generally made Django that much better:
Ahmad Al-Ibrahim
Ahmed Eltawela <https://github.com/ahmedabt>
Ahmed Nassar <https://ahmednassar7.github.io/>
Aina Anjary Fenomamy R. <anja1catus@gmail.com>
ajs <adi@sieker.info>
Akash Agrawal <akashrocksha@gmail.com>
Akash Kumar Sen <akashkumarsen4@gmail.com>
Expand Down
3 changes: 2 additions & 1 deletion django/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
17 changes: 17 additions & 0 deletions tests/forms_tests/field_tests/test_multiplechoicefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Loading