Django - Built in email validator failing

In my project, on the form, I apply the built in validate_email validator on a field.

I apply it to one of my form fields, however it throws an error when I run the server.

Here is the code:

from django.core.validators import MinLengthValidator, MaxLengthValidator, validate_email

from django import forms

class SignupForm(forms.Form):
    email = forms.EmailField(validators=[validate_email(value='Email wrong'), MinLengthValidator(3), 
    MaxLengthValidator(255)], widget=forms.EmailInput(attrs={'placeholder': 'Email Address'}))

The error I receive when server is run is:
django.core.exceptions.ValidationError: [‘Enter a valid email address.’]

However when I remove the brackets after validate_email, everything works ok, however I need them as I want to override the message value displayed. Does anybody know the issue? Thanks.

Adding brackets calls the validator function with that value. It doesn’t replace the error message.

Instead, create EmailValidator with a custom message:

... vaildators=[EmailValidator(message="Email wrong')]...

This calls the EmailValidator class and returns an instance, that then can be called to validate values. For example you can do this to get an error:

EmailValidator(message="Email wrong')('not-an-email')

Hope that helps,


Thank you Adam. Very helpful