Language

Valida che un valore sia un codice di lingua valido Unicode (come fr or zh-Hant).

Si applica a proprietà o metodo
Opzioni
Classe Symfony\Component\Validator\Constraints\Language
Validatore Symfony\Component\Validator\Constraints\LanguageValidator

Uso di base

  • YAML
    # src/UserBundle/Resources/config/validation.yml
    Acme\UserBundle\Entity\User:
        properties:
            preferredLanguage:
                - Language: ~
    
  • Annotazioni
    // src/Acme/UserBundle/Entity/User.php
    namespace Acme\UserBundle\Entity;
    
    use Symfony\Component\Validator\Constraints as Assert;
    
    class User
    {
        /**
         * @Assert\Language
         */
         protected $preferredLanguage;
    }
    
  • 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\UserBundle\Entity\User">
            <property name="preferredLanguage">
                <constraint name="Language" />
            </property>
        </class>
    </constraint-mapping>
    
  • PHP
    // src/Acme/UserBundle/Entity/User.php
    namespace Acme\UserBundle\Entity;
    
    use Symfony\Component\Validator\Mapping\ClassMetadata;
    use Symfony\Component\Validator\Constraints as Assert;
    
    class User
    {
        public static function loadValidatorMetadata(ClassMetadata $metadata)
        {
            $metadata->addPropertyConstraint('preferredLanguage', new Assert\Language());
        }
    }
    

Opzioni

message

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

Messaggio mostrato se la stringa non è un codice di lingua valido.

Tabella dei contenuti

Argomento precedente

UniqueEntity

Argomento successivo

Locale

Questa pagina