1

Currently the EmailType of Symfony allows any email like:

"whatever@domain"

So there is no validation against the domain of the email

what is the best way to make it accepts only email like:

"whatever@domain.something"

so that the domain should contain at least one dot.

Since it should be a common issue, I was wondering if there's already a built-in way to accomplish it.

Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
  • 2
    Since you are talking about `EmailType` instead of `EmailValidator`, are you talking about the html5 client-side validation? – Federkun Dec 23 '16 at 12:24

2 Answers2

5

Use Email constraint to validate email values correctly:

use Symfony\Component\Validator\Constraints as Assert;

class User 
{
    /**
     * @var string
     *
     * @Assert\Email()
     */
    private $email;
}

On submit the form the EmailValidator checks whether this '/^.+\@\S+\.\S+$/' pattern is valid or not, and throws a violation constraint if not.

yceruto
  • 9,230
  • 5
  • 38
  • 65
3

The HTML5 email type allows email such as example@localhost. If you want to be more stricter, you can add the regex to the HTML5 input. You can do that when you renders the HTML widget of the email field:

{{ form_widget(form.email, {'attr': {'pattern': '[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,63}$'}}) }}
Community
  • 1
  • 1
Federkun
  • 36,084
  • 8
  • 78
  • 90