|
@@ -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
|