Country

Valida che un valore sia un codice ISO 3166-1 alpha-2 valido per un paese.

Si applica a proprietà o metodo
Opzioni
Classe Symfony\Component\Validator\Constraints\Country
Validatore Symfony\Component\Validator\Constraints\CountryValidator

Uso di base

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

Opzioni

message

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

Messaggio mostrato se la stringa non è un codice valido per un paese.

Tabella dei contenuti

Argomento precedente

Locale

Argomento successivo

File

Questa pagina