diff options
| author | afenoum <anja1catus@gmail.com> | 2026-03-26 11:30:02 +0300 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2026-03-26 15:21:00 -0400 |
| commit | 8b7ea2bcdd7297941ca6beb72d93b9568e187349 (patch) | |
| tree | 524931260dec3d917809ab720fc229e9568102e7 | |
| parent | f6167b8bc881babd19b67c004e8f37954afc192e (diff) | |
Refs #36913 -- Maintained error message determinism in MultipleChoiceField.validate().
Used Django's OrderedSet datastructure instead of set() in MultipleChoiceField.validate()
to prevent submission ordering from being discarded during validation.
Thanks to Jacob Walls, JaeHyuck Sa, Jake Howard and Simon Charette for
the reviews.
| -rw-r--r-- | django/forms/fields.py | 5 | ||||
| -rw-r--r-- | tests/forms_tests/field_tests/test_multiplechoicefield.py | 4 |
2 files changed, 5 insertions, 4 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index 7f978aa03d..8aad2d48b8 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -40,6 +40,7 @@ from django.forms.widgets import ( ) from django.utils import formats from django.utils.choices import normalize_choices +from django.utils.datastructures import OrderedSet from django.utils.dateparse import parse_datetime, parse_duration from django.utils.duration import duration_string from django.utils.ipv6 import MAX_IPV6_ADDRESS_LENGTH, clean_ipv6_address @@ -965,8 +966,8 @@ class MultipleChoiceField(ChoiceField): 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. - # Use set() to avoid redundant validation. - for val in set(value): + # Avoid redundant validation, and keep elements ordered. + for val in OrderedSet(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 90a47ceee0..0ec53e1b30 100644 --- a/tests/forms_tests/field_tests/test_multiplechoicefield.py +++ b/tests/forms_tests/field_tests/test_multiplechoicefield.py @@ -91,5 +91,5 @@ class MultipleChoiceFieldTest(SimpleTestCase): def test_validate_duplicated_invalid_values(self): f = MultipleChoiceField(choices=[("1", "one"), ("2", "Two")]) - with self.assertRaises(ValidationError): - f.validate(["1", "1", "invalid", "invalid"]) + with self.assertRaisesMessage(ValidationError, "invalid-one"): + f.validate(["invalid-one", "invalid-one", "invalid-two"]) |
