Skip to content

Commit 9a2622e

Browse files
committed
Bug (sponsors): Default primary contact field to True for first contact
Set the initial `primary` contact value to True in the form, ensuring only the first contact is marked as primary. All additional contact forms added will default to False. Form validation remains unchanged in the event that the `primary` field is unchecked by the user. Closes #2610
1 parent 3ad7b96 commit 9a2622e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

sponsors/forms.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class Meta:
5454
max_num=5,
5555
)
5656

57+
formset = SponsorContactFormSet(
58+
initial=[{"primary": True}]
59+
)
60+
5761

5862
class SponsorshipsBenefitsForm(forms.Form):
5963
"""
@@ -285,7 +289,10 @@ def __init__(self, *args, **kwargs):
285289
if self.data:
286290
self.contacts_formset = SponsorContactFormSet(self.data, **formset_kwargs)
287291
else:
288-
self.contacts_formset = SponsorContactFormSet(**formset_kwargs)
292+
self.contacts_formset = SponsorContactFormSet(
293+
initial=[{"primary": True}],
294+
**formset_kwargs
295+
)
289296

290297
def clean(self):
291298
cleaned_data = super().clean()

sponsors/tests/test_forms.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,15 @@ def test_invalidate_form_if_no_primary_contact(self):
534534
msg = "You have to mark at least one contact as the primary one."
535535
self.assertIn(msg, form.errors["__all__"])
536536

537+
def test_initial_primary_contact(self):
538+
form = SponsorshipApplicationForm()
539+
formset = form.contacts_formset
540+
541+
self.assertTrue(
542+
formset.forms[0].initial.get("primary"),
543+
"The primary field in the first contact form should be initially set to True."
544+
)
545+
537546

538547
class SponsorContactFormSetTests(TestCase):
539548
def setUp(self):

0 commit comments

Comments
 (0)