From a1fe3aaccab532170bbac7c2683fc3a63d694fe0 Mon Sep 17 00:00:00 2001 From: CocoByte Date: Sun, 17 Dec 2023 18:14:08 -0700 Subject: [PATCH] Finally fixed! --- src/registrar/forms/application_wizard.py | 83 ++++++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/src/registrar/forms/application_wizard.py b/src/registrar/forms/application_wizard.py index 5310c4610..faf005d71 100644 --- a/src/registrar/forms/application_wizard.py +++ b/src/registrar/forms/application_wizard.py @@ -15,6 +15,7 @@ from registrar.templatetags.url_helpers import public_site_url from registrar.utility import errors logger = logging.getLogger(__name__) +from registrar.management.commands.utility.terminal_helper import TerminalColors, TerminalHelper class RegistrarForm(forms.Form): @@ -262,7 +263,7 @@ class OrganizationContactForm(RegistrarForm): validators=[ RegexValidator( "^[0-9]{5}(?:-[0-9]{4})?$|^$", - message="Enter a zip code in the required format, like 12345 or 12345-6789.", + message="Enter a zip code in the form of 12345 or 12345-6789.", ) ], ) @@ -557,6 +558,7 @@ class YourContactForm(RegistrarForm): class OtherContactsForm(RegistrarForm): first_name = forms.CharField( + # required=False, #is required, but validate in clean() instead to allow for custom form deletion condition label="First name / given name", error_messages={"required": "Enter the first name / given name of this contact."}, ) @@ -565,10 +567,12 @@ class OtherContactsForm(RegistrarForm): label="Middle name (optional)", ) last_name = forms.CharField( + # required=False, #is required, but validate in clean() instead to allow for custom form deletion condition label="Last name / family name", error_messages={"required": "Enter the last name / family name of this contact."}, ) title = forms.CharField( + # required=False, #is required, but validate in clean() instead to allow for custom form deletion condition label="Title or role in your organization", error_messages={ "required": ( @@ -577,27 +581,100 @@ class OtherContactsForm(RegistrarForm): }, ) email = forms.EmailField( + # required=False, #is required, but validate in clean() instead to allow for custom form deletion condition label="Email", error_messages={"invalid": ("Enter an email address in the required format, like name@example.com.")}, ) phone = PhoneNumberField( + # required=False, #is required, but validate in clean() instead to allow for custom form deletion condition label="Phone", error_messages={"required": "Enter a phone number for this contact."}, ) + + + + # Override clean in order to correct validation logic + def clean(self): + cleaned = self.cleaned_data # super().clean() + TerminalHelper.print_debug(f"""{TerminalColors.MAGENTA} CLEANING... + FIELDS: + {self.fields} + CLEANED: + {cleaned}{TerminalColors.ENDC}""") + + form_is_empty = all(v is None or v == '' for v in cleaned.values()) + # if not form_is_empty: #TODO: add check for "marked for deleteion" when implementing button + #Validation Logic + # if not self.cleaned_data["first_name"]: + # self.add_error('first_name', "Enter the first name / given name of this contact.") + # if not self.cleaned_data["last_name"]: + # self.add_error('last_name', "Enter the last name / family name of this contact.") + # if not self.cleaned_data["title"]: + # self.add_error('title', "Enter the title or role in your organization of this contact (e.g., Chief Information Officer).") + if form_is_empty: + TerminalHelper.print_debug(f"""{TerminalColors.MAGENTA} ***** BLANK FORM DETECTED ******* + {TerminalColors.ENDC}""") + + # clear any errors raised by the form fields + # (before this clean() method is run, each field + # performs its own clean, which could result in + # errors that we wish to ignore at this point) + # + # NOTE: we cannot just clear() the errors list. + # That causes problems. + for field in self.fields: + if field in self.errors: + del self.errors[field] + return cleaned + + + # # Always return a value to use as the new cleaned data, even if + # # this method didn't change it. + # return data + + # for field in self.fields.values(): + # isBlank = field is None or field == '' + # TerminalHelper.print_debug(f"""{TerminalColors.YELLOW} {field} is blank = {isBlank} {TerminalColors.ENDC}""") + + + # TerminalHelper.print_debug(f"""{TerminalColors.YELLOW} {field.required} {TerminalColors.ENDC}""") + # return None + # return super().clean() + + # def _should_delete_form(self, form): + # TerminalHelper.print_debug(f"{TerminalColors.MAGENTA} SHOULD DELETE FORM?...{TerminalColors.ENDC}") + # """Return whether or not the form was marked for deletion.""" + # return all(field is None or field == '' for field in self.fields.values()) class BaseOtherContactsFormSet(RegistrarFormSet): JOIN = "other_contacts" + # def get_deletion_widget(self): + # delete_button = '' + # return delete_button + + # # if form.cleaned_data.get(forms.formsets.DELETION_FIELD_NAME): + # # return True # marked for delete + # # fields = ('name', 'question', 'amount', 'measure', 'comment') + # print(form.cleaned_data) + # if not any(form.cleaned_data): + # return True + # return False + def should_delete(self, cleaned): - empty = (isinstance(v, str) and not v.strip() for v in cleaned.values()) + # TerminalHelper.print_debug(f"{TerminalColors.MAGENTA} SHOULD DELETE OTHER CONTACTS?... {cleaned.values()}{TerminalColors.ENDC}") + empty = (isinstance(v, str) and (v.strip() == "" or v == None) for v in cleaned.values()) return all(empty) + def to_database(self, obj: DomainApplication): + TerminalHelper.print_debug(f"{TerminalColors.OKCYAN} TO DATABASE {TerminalColors.ENDC}") self._to_database(obj, self.JOIN, self.should_delete, self.pre_update, self.pre_create) @classmethod def from_database(cls, obj): + TerminalHelper.print_debug(f"{TerminalColors.OKCYAN} FROM DATABASE {TerminalColors.ENDC}") return super().from_database(obj, cls.JOIN, cls.on_fetch) @@ -606,6 +683,7 @@ OtherContactsFormSet = forms.formset_factory( extra=1, absolute_max=1500, # django default; use `max_num` to limit entries formset=BaseOtherContactsFormSet, + # can_delete=True #TODO: use when implementing delete button? ) @@ -634,7 +712,6 @@ class AnythingElseForm(RegistrarForm): ], ) - class RequirementsForm(RegistrarForm): is_policy_acknowledged = forms.BooleanField( label="I read and agree to the requirements for operating .gov domains.",