diff --git a/src/registrar/forms/domain.py b/src/registrar/forms/domain.py index 0bfd9b667..7b0ac2956 100644 --- a/src/registrar/forms/domain.py +++ b/src/registrar/forms/domain.py @@ -2,7 +2,7 @@ import logging from django import forms -from django.core.validators import MinValueValidator, MaxValueValidator, RegexValidator +from django.core.validators import MinValueValidator, MaxValueValidator, RegexValidator, MaxLengthValidator from django.forms import formset_factory from registrar.models import DomainRequest from phonenumber_field.widgets import RegionalPhoneNumberWidget @@ -31,7 +31,17 @@ logger = logging.getLogger(__name__) class DomainAddUserForm(forms.Form): """Form for adding a user to a domain.""" - email = forms.EmailField(label="Email") + email = forms.EmailField( + label="Email", + max_length=None, + error_messages={"invalid": ("Enter your email address in the required format, like name@example.com.")}, + validators=[ + MaxLengthValidator( + 320, + message="Response must be less than 320 characters.", + ) + ], + ) def clean(self): """clean form data by lowercasing email""" @@ -171,6 +181,8 @@ NameserverFormset = formset_factory( class ContactForm(forms.ModelForm): """Form for updating contacts.""" + email = forms.EmailField(max_length=None) + class Meta: model = Contact fields = ["first_name", "middle_name", "last_name", "title", "email", "phone"] @@ -194,6 +206,10 @@ class ContactForm(forms.ModelForm): # which interferes with out input_with_errors template tag self.fields["phone"].widget.attrs.pop("maxlength", None) + # Define a custom validator for the email field with a custom error message + email_max_length_validator = MaxLengthValidator(320, message="Response must be less than 320 characters.") + self.fields["email"].validators.append(email_max_length_validator) + for field_name in self.required: self.fields[field_name].required = True @@ -291,10 +307,17 @@ class DomainSecurityEmailForm(forms.Form): security_email = forms.EmailField( label="Security email (optional)", + max_length=None, required=False, error_messages={ "invalid": str(SecurityEmailError(code=SecurityEmailErrorCodes.BAD_DATA)), }, + validators=[ + MaxLengthValidator( + 320, + message="Response must be less than 320 characters.", + ) + ], ) diff --git a/src/registrar/forms/domain_request_wizard.py b/src/registrar/forms/domain_request_wizard.py index 1efc028f6..c3ac3b4c2 100644 --- a/src/registrar/forms/domain_request_wizard.py +++ b/src/registrar/forms/domain_request_wizard.py @@ -369,7 +369,14 @@ class AuthorizingOfficialForm(RegistrarForm): ) email = forms.EmailField( label="Email", + max_length=None, error_messages={"invalid": ("Enter an email address in the required format, like name@example.com.")}, + validators=[ + MaxLengthValidator( + 320, + message="Response must be less than 320 characters.", + ) + ], ) @@ -566,7 +573,14 @@ class YourContactForm(RegistrarForm): ) email = forms.EmailField( label="Email", + max_length=None, error_messages={"invalid": ("Enter your email address in the required format, like name@example.com.")}, + validators=[ + MaxLengthValidator( + 320, + message="Response must be less than 320 characters.", + ) + ], ) phone = PhoneNumberField( label="Phone", @@ -621,10 +635,17 @@ class OtherContactsForm(RegistrarForm): ) email = forms.EmailField( label="Email", + max_length=None, error_messages={ "required": ("Enter an email address in the required format, like name@example.com."), "invalid": ("Enter an email address in the required format, like name@example.com."), }, + validators=[ + MaxLengthValidator( + 320, + message="Response must be less than 320 characters.", + ) + ], ) phone = PhoneNumberField( label="Phone", diff --git a/src/registrar/migrations/0083_alter_contact_email_alter_publiccontact_email.py b/src/registrar/migrations/0083_alter_contact_email_alter_publiccontact_email.py new file mode 100644 index 000000000..c58c09656 --- /dev/null +++ b/src/registrar/migrations/0083_alter_contact_email_alter_publiccontact_email.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.10 on 2024-04-09 16:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("registrar", "0082_domaininformation_organization_type_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="contact", + name="email", + field=models.EmailField(blank=True, db_index=True, max_length=320, null=True), + ), + migrations.AlterField( + model_name="publiccontact", + name="email", + field=models.EmailField(help_text="Contact's email address", max_length=320), + ), + ] diff --git a/src/registrar/models/contact.py b/src/registrar/models/contact.py index d3de5a293..6bc20ebeb 100644 --- a/src/registrar/models/contact.py +++ b/src/registrar/models/contact.py @@ -40,6 +40,7 @@ class Contact(TimeStampedModel): null=True, blank=True, db_index=True, + max_length=320, ) phone = PhoneNumberField( null=True, diff --git a/src/registrar/models/public_contact.py b/src/registrar/models/public_contact.py index f9dea3f02..00d065404 100644 --- a/src/registrar/models/public_contact.py +++ b/src/registrar/models/public_contact.py @@ -68,7 +68,7 @@ class PublicContact(TimeStampedModel): sp = models.CharField(null=False, help_text="Contact's state or province") pc = models.CharField(null=False, help_text="Contact's postal code") cc = models.CharField(null=False, help_text="Contact's country code") - email = models.EmailField(null=False, help_text="Contact's email address") + email = models.EmailField(null=False, help_text="Contact's email address", max_length=320) voice = models.CharField(null=False, help_text="Contact's phone number. Must be in ITU.E164.2005 format") fax = models.CharField( null=True,