Fixed (with a bandaid)

This commit is contained in:
CocoByte 2023-12-29 02:11:19 -07:00
parent ac3afe436c
commit affc35398d
No known key found for this signature in database
GPG key ID: BBFAA2526384C97F
2 changed files with 53 additions and 5 deletions

View file

@ -9,6 +9,7 @@ from django.core.validators import RegexValidator, MaxLengthValidator
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from api.views import DOMAIN_API_MESSAGES from api.views import DOMAIN_API_MESSAGES
from registrar.management.commands.utility.terminal_helper import TerminalColors
from registrar.models import Contact, DomainApplication, DraftDomain, Domain from registrar.models import Contact, DomainApplication, DraftDomain, Domain
from registrar.templatetags.url_helpers import public_site_url from registrar.templatetags.url_helpers import public_site_url
@ -262,7 +263,7 @@ class OrganizationContactForm(RegistrarForm):
validators=[ validators=[
RegexValidator( RegexValidator(
"^[0-9]{5}(?:-[0-9]{4})?$|^$", "^[0-9]{5}(?:-[0-9]{4})?$|^$",
message="Enter a zip code in the form of 12345 or 12345-6789.", message="Enter a zip code in the required format, like 12345 or 12345-6789.",
) )
], ],
) )
@ -585,11 +586,52 @@ class OtherContactsForm(RegistrarForm):
error_messages={"required": "Enter a phone number for this contact."}, error_messages={"required": "Enter a phone number for this contact."},
) )
# Override clean in order to correct validation logic # Override clean in order to correct validation logic
def clean(self): def clean(self):
# NOTE: using self.cleaned_data directly apparently causes a CORS error # NOTE: using self.cleaned_data directly apparently causes a CORS error
cleaned = super().clean() cleaned = super().clean()
form_is_empty = all(v is None or v == "" for v in cleaned.values())
logger.info(f"""
{TerminalColors.MAGENTA}form data:
{TerminalColors.OKBLUE}{self.data}
{TerminalColors.MAGENTA}form cleaned:
{TerminalColors.OKBLUE}{cleaned}
{self.data.items}
{TerminalColors.ENDC}
""")
# for f in self.fields:
# logger.info(f"""
# {TerminalColors.YELLOW}{f}
# {self.data.get(f)}
# {TerminalColors.ENDC}
# """)
form_is_empty = all(v is None or v == "" for v in cleaned.values())
# NOTE: Phone number and email do NOT show up in cleaned values.
# I have spent hours tyring to figure out why, but have no idea...
# so for now we will grab their values from the raw data...
for i in self.data:
if 'phone' in i or 'email' in i:
field_value = self.data.get(i)
logger.info(f"""
{TerminalColors.YELLOW}{i}
{self.data.get(i)}
{TerminalColors.ENDC}
""")
form_is_empty = field_value == "" or field_value is None
logger.info(f"""
{TerminalColors.OKCYAN}empty? {form_is_empty}
{TerminalColors.ENDC}
""")
if form_is_empty: if form_is_empty:
# clear any errors raised by the form fields # clear any errors raised by the form fields
# (before this clean() method is run, each field # (before this clean() method is run, each field
@ -599,8 +641,14 @@ class OtherContactsForm(RegistrarForm):
# NOTE: we cannot just clear() the errors list. # NOTE: we cannot just clear() the errors list.
# That causes problems. # That causes problems.
for field in self.fields: for field in self.fields:
if field in self.errors: if field in self.errors: # and field in cleaned
logger.info(f"""
{TerminalColors.FAIL}removing {field}
{TerminalColors.ENDC}
""")
del self.errors[field] del self.errors[field]
return cleaned return cleaned

View file

@ -216,7 +216,7 @@ class TestFormValidation(MockEppLib):
def test_other_contact_email_invalid(self): def test_other_contact_email_invalid(self):
"""must be a valid email address.""" """must be a valid email address."""
form = OtherContactsForm(data={"email": "boss@boss"}) form = OtherContactsForm(data={"email": "splendid@boss"})
self.assertEqual( self.assertEqual(
form.errors["email"], form.errors["email"],
["Enter an email address in the required format, like name@example.com."], ["Enter an email address in the required format, like name@example.com."],
@ -224,7 +224,7 @@ class TestFormValidation(MockEppLib):
def test_other_contact_phone_invalid(self): def test_other_contact_phone_invalid(self):
"""Must be a valid phone number.""" """Must be a valid phone number."""
form = OtherContactsForm(data={"phone": "boss@boss"}) form = OtherContactsForm(data={"phone": "super@boss"})
self.assertTrue(form.errors["phone"][0].startswith("Enter a valid phone number ")) self.assertTrue(form.errors["phone"][0].startswith("Enter a valid phone number "))
def test_requirements_form_blank(self): def test_requirements_form_blank(self):