From 2e737bf4d8b4472cfd994e6633b64caf59b0513f Mon Sep 17 00:00:00 2001 From: David Kennedy Date: Thu, 4 Jan 2024 06:36:20 -0500 Subject: [PATCH] handled form initialization --- src/registrar/assets/js/get-gov.js | 6 ++++- src/registrar/forms/application_wizard.py | 26 +++++++++++++++------- src/registrar/models/domain_application.py | 6 ++--- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/registrar/assets/js/get-gov.js b/src/registrar/assets/js/get-gov.js index c8e561d75..32008d0c7 100644 --- a/src/registrar/assets/js/get-gov.js +++ b/src/registrar/assets/js/get-gov.js @@ -506,7 +506,11 @@ function toggleTwoDomElements(ele1, ele2, index) { function handleRadioButtonChange() { // Check the value of the selected radio button - let selectedValue = document.querySelector('input[name="other_contacts-has_other_contacts"]:checked').value; + // Attempt to find the radio button element that is checked + let radioButtonChecked = document.querySelector('input[name="other_contacts-has_other_contacts"]:checked'); + + // Check if the element exists before accessing its value + let selectedValue = radioButtonChecked ? radioButtonChecked.value : null; switch (selectedValue) { case 'True': diff --git a/src/registrar/forms/application_wizard.py b/src/registrar/forms/application_wizard.py index 39b777b1a..c28e46162 100644 --- a/src/registrar/forms/application_wizard.py +++ b/src/registrar/forms/application_wizard.py @@ -571,14 +571,24 @@ class YourContactForm(RegistrarForm): class OtherContactsYesNoForm(RegistrarForm): - has_other_contacts = forms.TypedChoiceField( - coerce=lambda x: x.lower() == 'true', - choices=( - (True, "Yes, I can name other employees."), - (False, "No (We'll ask you to explain why).") - ), - widget=forms.RadioSelect - ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + if self.application and self.application.has_other_contacts(): + default_value = True + elif self.application and self.application.has_rationale(): + default_value = False + else: + default_value = None + + self.fields['has_other_contacts'] = forms.TypedChoiceField( + coerce=lambda x: x.lower() == 'true' if x is not None else None, + choices=( + (True, "Yes, I can name other employees."), + (False, "No (We'll ask you to explain why).") + ), + initial=default_value, + widget=forms.RadioSelect + ) def is_valid(self): val = super().is_valid() diff --git a/src/registrar/models/domain_application.py b/src/registrar/models/domain_application.py index 417efe0e5..7f1f39cd7 100644 --- a/src/registrar/models/domain_application.py +++ b/src/registrar/models/domain_application.py @@ -831,9 +831,9 @@ class DomainApplication(TimeStampedModel): DomainApplication.OrganizationChoices.INTERSTATE, ] - def show_no_other_contacts_rationale(self) -> bool: - """Show this step if the other contacts are blank.""" - return not self.other_contacts.exists() + def has_rationale(self) -> bool: + """Does this application have no_other_contacts_rationale""" + return bool(self.no_other_contacts_rationale) def has_other_contacts(self) -> bool: """Does this application have other contacts listed?"""