summaryrefslogtreecommitdiff
path: root/tests/forms_tests
diff options
context:
space:
mode:
authorMarc SeguĂ­ Coll <metarizard@gmail.com>2022-05-08 00:53:13 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-05-10 13:42:31 +0200
commit262fde94de5eb6544fc0f289575583436613c045 (patch)
treed0b5f41d41a4e4d86a2973bfb260350e4dab8c0e /tests/forms_tests
parent667105877e6723c6985399803a364848891513cc (diff)
Fixed #33622 -- Allowed customizing error messages for invalid number of forms.
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Diffstat (limited to 'tests/forms_tests')
-rw-r--r--tests/forms_tests/tests/test_formsets.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py
index c713c85bfb..0868b41644 100644
--- a/tests/forms_tests/tests/test_formsets.py
+++ b/tests/forms_tests/tests/test_formsets.py
@@ -404,6 +404,37 @@ class FormsFormsetTestCase(SimpleTestCase):
'<ul class="errorlist nonform"><li>Please submit at most 1 form.</li></ul>',
)
+ def test_formset_validate_max_flag_custom_error(self):
+ data = {
+ "choices-TOTAL_FORMS": "2",
+ "choices-INITIAL_FORMS": "0",
+ "choices-MIN_NUM_FORMS": "0",
+ "choices-MAX_NUM_FORMS": "2",
+ "choices-0-choice": "Zero",
+ "choices-0-votes": "0",
+ "choices-1-choice": "One",
+ "choices-1-votes": "1",
+ }
+ ChoiceFormSet = formset_factory(Choice, extra=1, max_num=1, validate_max=True)
+ formset = ChoiceFormSet(
+ data,
+ auto_id=False,
+ prefix="choices",
+ error_messages={
+ "too_many_forms": "Number of submitted forms should be at most %(num)d."
+ },
+ )
+ self.assertFalse(formset.is_valid())
+ self.assertEqual(
+ formset.non_form_errors(),
+ ["Number of submitted forms should be at most 1."],
+ )
+ self.assertEqual(
+ str(formset.non_form_errors()),
+ '<ul class="errorlist nonform">'
+ "<li>Number of submitted forms should be at most 1.</li></ul>",
+ )
+
def test_formset_validate_min_flag(self):
"""
If validate_min is set and min_num is more than TOTAL_FORMS in the
@@ -431,6 +462,37 @@ class FormsFormsetTestCase(SimpleTestCase):
"Please submit at least 3 forms.</li></ul>",
)
+ def test_formset_validate_min_flag_custom_formatted_error(self):
+ data = {
+ "choices-TOTAL_FORMS": "2",
+ "choices-INITIAL_FORMS": "0",
+ "choices-MIN_NUM_FORMS": "0",
+ "choices-MAX_NUM_FORMS": "0",
+ "choices-0-choice": "Zero",
+ "choices-0-votes": "0",
+ "choices-1-choice": "One",
+ "choices-1-votes": "1",
+ }
+ ChoiceFormSet = formset_factory(Choice, extra=1, min_num=3, validate_min=True)
+ formset = ChoiceFormSet(
+ data,
+ auto_id=False,
+ prefix="choices",
+ error_messages={
+ "too_few_forms": "Number of submitted forms should be at least %(num)d."
+ },
+ )
+ self.assertFalse(formset.is_valid())
+ self.assertEqual(
+ formset.non_form_errors(),
+ ["Number of submitted forms should be at least 3."],
+ )
+ self.assertEqual(
+ str(formset.non_form_errors()),
+ '<ul class="errorlist nonform">'
+ "<li>Number of submitted forms should be at least 3.</li></ul>",
+ )
+
def test_formset_validate_min_unchanged_forms(self):
"""
min_num validation doesn't consider unchanged forms with initial data