Skip to content

Commit

Permalink
Merge pull request #1826 from MTG/signup-duplicate-email
Browse files Browse the repository at this point in the history
If email1 validation fails, remove email2 validation errors
  • Loading branch information
ffont authored Feb 11, 2025
2 parents 59e31e5 + 93387e2 commit 7fa5f2f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,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):
Expand Down
5 changes: 5 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7fa5f2f

Please sign in to comment.