How do I change the message that pops up as a ValidationError in ModelForm field?

Hi there,

I have a ModelForm which have fields only from Model, no one it’s own. Now, I want to change the message that appears as ValidationError on the EmailField, when we miss the @ symbol in email, see the screenshot below:

I want to change the message encircled in red to "Enter a valid email.".

I did the following in my ModelForm:

from django.utils.translation import gettext as _

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ("email",)
        error_messages = {
            'email': {
                'invalid': _("Enter a valid email.")

But, it’s not working. Anyone knows how to change this message?

Hey there!
The screenshot you just posted it’s not being provided by Django, but directly from the Browser.
If you check this at HTML email validation you will going to see that’s the exact same message.

So the form that you’re trying to submit, is not submitted because of this validator. The request is not even going to the server, and that’s good, because the browser will validate that the email is at least partially valid.

@leandrodesouzadev I know that why the form is not being submitted. It’s not the issue. Actually, I want to change this validation message from "Please include an '@' in the email address. 'emaiil' is missing an '@'." to just a simple message “Enter a valid email.”`, that’s all I want to do right now. How can I accomplish this?

You can’t change that message.
Do you want to do all the validation on the server rather than in the browser?

If so, you must override in the form the email field from EmailField to CharField. Then, for any value the form will be submitted, and then you can do your custom validation on your own.
Personally, I would not do that.

See Client-side form validation - Learn web development | MDN for how you can change that message and the limitations associated with doing so.

1 Like

Oh i didnt know that, thanks @KenWhitesell!