Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages in full color showing how to combine Symfony with Docker, APIs, queues & async tasks, Webpack, Single-Page Applications, etc.

Buy printed version

New in Symfony 4.1: HTML5 Email Validation

Contributed by
Billie Thompson
in #24442.

Symfony 4.1 will be released in May 2018. This is the first article of the series that shows the most important new features introduced by this Symfony version.

Validation of email addresses is one of those never-ending debates among developers. Some argue that you can't truly validate them programmatically and that you must always send a confirmation email. Others argue that RFC 5322 compliant programmatic validation is enough for real-world applications.

Symfony always made this choice yours with the strict option of the Email validation constraint. When false, the email address is validated against a simple regular expression. If true, a RFC compliant validation is done using the egulias/email-validator third-party library.

However, the regular expression used to validate email addresses was too simple. That's why in Symfony 4.1 we decided to start using the same email validation done by HTML5. In practice, we've deprecated the strict option in favor of a new mode option with these values:

  • loose: uses a simple regular expression to find the most obvious mistakes (not including a @ character, etc.). It's like the previous strict = false.
  • strict: performs the RFC compliant validation and requires to install the egulias/email-validator library. It's like the previous strict = true.
  • html5 uses the same regular expression of the HTML5 specification. It's the best balanced value, providing fast and good validation without requiring external libraries.

Configuring the email validation in the framework

Contributed by
Christian Flothmann
in #25478.

When using the Validator component inside a Symfony application, you can set the email address validation mode with the new email_validation_mode option:

# config/packages/framework.yaml
        # possible values: 'loose', 'strict', 'html5'
        email_validation_mode: 'html5'


This is great! Now lets see the bounces drop :-)
Just to clarify, if not email_validation_mode not specificed, html5 will be the default used?
@Martin the default value is `loose` to keep backward compatibility with previous versions.

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.