From 9a2622eb77f65bfcbe30621b922bd01cb254f392 Mon Sep 17 00:00:00 2001 From: dorian-adams Date: Sat, 26 Oct 2024 03:13:01 -0400 Subject: [PATCH] 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 --- sponsors/forms.py | 9 ++++++++- sponsors/tests/test_forms.py | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sponsors/forms.py b/sponsors/forms.py index 4ced017c9..d1635f8ea 100644 --- a/sponsors/forms.py +++ b/sponsors/forms.py @@ -54,6 +54,10 @@ class Meta: max_num=5, ) +formset = SponsorContactFormSet( + initial=[{"primary": True}] +) + class SponsorshipsBenefitsForm(forms.Form): """ @@ -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() diff --git a/sponsors/tests/test_forms.py b/sponsors/tests/test_forms.py index 49b0515cd..c375b9a2b 100644 --- a/sponsors/tests/test_forms.py +++ b/sponsors/tests/test_forms.py @@ -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):