DateTime

Valida che un valore sia una data/ora valida, cioè o un oggetto DateTime o una stringa (o un oggetto che possa essere forzato a stringa) con un formato valido YYYY-MM-DD HH:MM:SS.

Si applica a proprietà o metodo
Opzioni
Classe Symfony\Component\Validator\Constraints\DateTime
Validatore Symfony\Component\Validator\Constraints\DateTimeValidator

Uso di base

  • YAML
    # src/Acme/BlogBundle/Resources/config/validation.yml
    Acme\BlogBundle\Entity\Author:
        properties:
            createdAt:
                - DateTime: ~
    
  • Annotazioni
    // src/Acme/BlogBundle/Entity/Author.php
    namespace Acme\BlogBundle\Entity;
    
    use Symfony\Component\Validator\Constraints as Assert;
    
    class Author
    {
        /**
         * @Assert\DateTime()
         */
         protected $createdAt;
    }
    
  • XML
    <!-- src/Acme/UserBundle/Resources/config/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 http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
    
        <class name="Acme\BlogBundle\Entity\Author">
            <property name="createdAt">
                <constraint name="DateTime" />
            </property>
        </class>
    </constraint-mapping>
    
  • PHP
    // src/Acme/BlogBundle/Entity/Author.php
    namespace Acme\BlogBundle\Entity;
    
    use Symfony\Component\Validator\Mapping\ClassMetadata;
    use Symfony\Component\Validator\Constraints as Assert;
    
    class Author
    {
        public static function loadValidatorMetadata(ClassMetadata $metadata)
        {
            $metadata->addPropertyConstraint('createdAt', new Assert\DateTime());
        }
    }
    

Opzioni

message

tipo: stringa predefinito: This value is not a valid datetime

Messaggio mostrato se i dati sottostanti non sono una data/ora valida.

Tabella dei contenuti

Argomento precedente

Date

Argomento successivo

Time

Questa pagina