DateTime ======== Validates that a value is a valid "datetime", meaning a string (or an object that can be cast into a string) that follows a specific format. ========== =================================================================== Applies to :ref:`property or method ` Options - `format`_ - `groups`_ - `message`_ - `payload`_ Class :class:`Symfony\\Component\\Validator\\Constraints\\DateTime` Validator :class:`Symfony\\Component\\Validator\\Constraints\\DateTimeValidator` ========== =================================================================== 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\DateTime * @var string A "Y-m-d H:i:s" formatted value */ protected $createdAt; } .. code-block:: yaml # config/validator/validation.yaml App\Entity\Author: properties: createdAt: - DateTime: ~ .. code-block:: xml .. 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 { /** * @var string A "Y-m-d H:i:s" formatted value */ protected $createdAt; public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('createdAt', new Assert\DateTime()); } } .. include:: /reference/constraints/_empty-values-are-valid.rst.inc Options ------- format ~~~~~~ **type**: ``string`` **default**: ``Y-m-d H:i:s`` This option allows to validate a custom date format. See :phpmethod:`DateTime::createFromFormat` for formatting options. .. include:: /reference/constraints/_groups-option.rst.inc ``message`` ~~~~~~~~~~~ **type**: ``string`` **default**: ``This value is not a valid datetime.`` This message is shown if the underlying data is not a valid datetime. You can use the following parameters in this message: =============== ============================================================== Parameter Description =============== ============================================================== ``{{ value }}`` The current (invalid) value =============== ============================================================== .. include:: /reference/constraints/_payload-option.rst.inc