From 93387e26ee6649d959ac8a65526b86139ed4b20b Mon Sep 17 00:00:00 2001 From: Alastair Porter Date: Fri, 31 Jan 2025 13:11:17 +0100 Subject: [PATCH] If email1 validation fails, remove email2 validation errors email1 validation checks to see if the email address is used email2 validation checks if the values are the same. If email1 fails then the cleaned value is always different to email2 and the check fails. Remove the check and the value for email2 in this case --- accounts/forms.py | 10 ++++++++++ accounts/views.py | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/accounts/forms.py b/accounts/forms.py index 61ff350ae..863f89ddf 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -219,6 +219,16 @@ def __init__(self, *args, **kwargs): self.fields['password1'].widget.attrs['placeholder'] = 'Password' self.fields['accepted_tos'].widget.attrs['class'] = 'bw-checkbox' + def clean(self): + # In the case that clean_email1 fails, the check for clean_email2 will report as "the email addresses are not the same" + # Therefore, in this specific case we remove the email2 error message because it doesn't make sense to + # have email1 say "you cannot use this email to create an account" and email2 say "the email addresses are not the same" + cleaned_data = super().clean() + if self.has_error("email1") and "email2" in self.errors: + del self.errors["email2"] + return cleaned_data + + def clean_username(self): username = self.cleaned_data["username"] if not username_taken_by_other_user(username): diff --git a/accounts/views.py b/accounts/views.py index f4976519d..f43d28669 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -318,6 +318,11 @@ def registration_modal(request): # If the form is NOT valid we return the Django rendered HTML version of the # registration modal (which includes the form and error messages) so the browser can show the updated # modal contents to the user + if form.has_error("email1") and "email2" in form.data: + # If email1 has an error, remove the value of email2 + post_data = request.POST.copy() + post_data["email2"] = "" + form = RegistrationForm(post_data) return render(request, 'accounts/modal_registration.html', {'registration_form': form}) else: form = RegistrationForm()