Skip to content

Latest commit

 

History

History
135 lines (98 loc) · 4.6 KB

Email.rst

File metadata and controls

135 lines (98 loc) · 4.6 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>`
Class :class:`Symfony\\Component\\Validator\\Constraints\\Email`
Validator :class:`Symfony\\Component\\Validator\\Constraints\\EmailValidator`

Basic Usage

.. configuration-block::

    .. 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 string $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): void
            {
                $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

mode

type: string default: html5

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

Tip

The possible values of this option are also defined as PHP constants of :class:`Symfony\\Component\\Validator\\Constraints\\Email` (e.g. Email::VALIDATION_MODE_STRICT).

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