Skip to content

Latest commit

 

History

History
155 lines (115 loc) · 4.94 KB

File metadata and controls

155 lines (115 loc) · 4.94 KB

Email

Validates that a value is a valid email address. The underlying value is cast to a string before being validated.

Applies to :ref:`property or method <validation-property-target>`
Options
Class :class:`Symfony\\Component\\Validator\\Constraints\\Email`
Validator :class:`Symfony\\Component\\Validator\\Constraints\\EmailValidator`

Basic Usage

.. configuration-block::

    .. code-block:: php-annotations

        // src/Entity/Author.php
        namespace App\Entity;

        use Symfony\Component\Validator\Constraints as Assert;

        class Author
        {
            /**
             * @Assert\Email(
             *     message = "The email '{{ value }}' is not a valid email."
             * )
             */
            protected $email;
        }

    .. code-block:: php-attributes

        // src/Entity/Author.php
        namespace App\Entity;

        use Symfony\Component\Validator\Constraints as Assert;

        class Author
        {
            #[Assert\Email(
                message: 'The email {{ value }} is not a valid email.',
            )]
            protected $email;
        }

    .. code-block:: yaml

        # config/validator/validation.yaml
        App\Entity\Author:
            properties:
                email:
                    - Email:
                        message: The email "{{ value }}" is not a valid email.

    .. code-block:: xml

        <!-- config/validator/validation.xml -->
        <?xml version="1.0" encoding="UTF-8" ?>
        <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

            <class name="App\Entity\Author">
                <property name="email">
                    <constraint name="Email">
                        <option name="message">The email "{{ value }}" is not a valid email.</option>
                    </constraint>
                </property>
            </class>
        </constraint-mapping>

    .. code-block:: php

        // src/Entity/Author.php
        namespace App\Entity;

        use Symfony\Component\Validator\Constraints as Assert;
        use Symfony\Component\Validator\Mapping\ClassMetadata;

        class Author
        {
            public static function loadValidatorMetadata(ClassMetadata $metadata)
            {
                $metadata->addPropertyConstraint('email', new Assert\Email([
                    'message' => 'The email "{{ value }}" is not a valid email.',
                ]));
            }
        }

Options

message

type: string default: This value is not a valid email address.

This message is shown if the underlying data is not a valid email address.

You can use the following parameters in this message:

Parameter Description
{{ value }} The current (invalid) value
{{ label }} Corresponding form field label
.. versionadded:: 5.2

    The ``{{ label }}`` parameter was introduced in Symfony 5.2.

mode

type: string default: (see below)

This option defines the pattern used to validate the email address. Valid values are:

  • loose uses a simple regular expression (just checks that at least one @ character is present, etc.). This validation is very simple and it's recommended to use one of the other modes instead;
  • html5 uses the same regular expression as the HTML5 email input element, making the backend validation consistent with the one provided by browsers;
  • strict uses the egulias/email-validator library (which you must install separately) for validation according to RFC 5322.

The default value used by this option is set in the :ref:`framework.validation.email_validation_mode <reference-validation-email_validation_mode>` configuration option.