Skip to content

Commit

Permalink
Bug (sponsors): Default primary contact field to True for first contact
Browse files Browse the repository at this point in the history
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 python#2610
  • Loading branch information
dorian-adams committed Oct 26, 2024
1 parent 3ad7b96 commit 9a2622e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sponsors/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class Meta:
max_num=5,
)

formset = SponsorContactFormSet(
initial=[{"primary": True}]
)


class SponsorshipsBenefitsForm(forms.Form):
"""
Expand Down Expand Up @@ -285,7 +289,10 @@ def __init__(self, *args, **kwargs):
if self.data:
self.contacts_formset = SponsorContactFormSet(self.data, **formset_kwargs)
else:
self.contacts_formset = SponsorContactFormSet(**formset_kwargs)
self.contacts_formset = SponsorContactFormSet(
initial=[{"primary": True}],
**formset_kwargs
)

def clean(self):
cleaned_data = super().clean()
Expand Down
9 changes: 9 additions & 0 deletions sponsors/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,15 @@ def test_invalidate_form_if_no_primary_contact(self):
msg = "You have to mark at least one contact as the primary one."
self.assertIn(msg, form.errors["__all__"])

def test_initial_primary_contact(self):
form = SponsorshipApplicationForm()
formset = form.contacts_formset

self.assertTrue(
formset.forms[0].initial.get("primary"),
"The primary field in the first contact form should be initially set to True."
)


class SponsorContactFormSetTests(TestCase):
def setUp(self):
Expand Down

0 comments on commit 9a2622e

Please sign in to comment.