Url === Validates that a value is a valid URL string. +----------------+---------------------------------------------------------------------+ | Applies to | :ref:`property or method ` | +----------------+---------------------------------------------------------------------+ | Options | - `message`_ | | | - `protocols`_ | | | - `payload`_ | | | - `checkDNS`_ | | | - `dnsMessage`_ | +----------------+---------------------------------------------------------------------+ | Class | :class:`Symfony\\Component\\Validator\\Constraints\\Url` | +----------------+---------------------------------------------------------------------+ | Validator | :class:`Symfony\\Component\\Validator\\Constraints\\UrlValidator` | +----------------+---------------------------------------------------------------------+ Basic Usage ----------- .. configuration-block:: .. code-block:: php-annotations // src/AppBundle/Entity/Author.php namespace AppBundle\Entity; use Symfony\Component\Validator\Constraints as Assert; class Author { /** * @Assert\Url() */ protected $bioUrl; } .. code-block:: yaml # src/AppBundle/Resources/config/validation.yml AppBundle\Entity\Author: properties: bioUrl: - Url: ~ .. code-block:: xml .. code-block:: php // src/AppBundle/Entity/Author.php namespace AppBundle\Entity; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints as Assert; class Author { public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('bioUrl', new Assert\Url()); } } Options ------- message ~~~~~~~ **type**: ``string`` **default**: ``This value is not a valid URL.`` This message is shown if the URL is invalid. .. configuration-block:: .. code-block:: php-annotations // src/AppBundle/Entity/Author.php namespace AppBundle\Entity; use Symfony\Component\Validator\Constraints as Assert; class Author { /** * @Assert\Url( * message = "The url '{{ value }}' is not a valid url", * ) */ protected $bioUrl; } .. code-block:: yaml # src/AppBundle/Resources/config/validation.yml AppBundle\Entity\Author: properties: bioUrl: - Url: message: The url "{{ value }}" is not a valid url. .. code-block:: xml .. code-block:: php // src/AppBundle/Entity/Author.php namespace AppBundle\Entity; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints as Assert; class Author { public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('bioUrl', new Assert\Url(array( 'message' => 'The url "{{ value }}" is not a valid url.', ))); } } protocols ~~~~~~~~~ **type**: ``array`` **default**: ``array('http', 'https')`` The protocols considered to be valid for the URL. For example, if you also consider the ``ftp://`` type URLs to be valid, redefine the ``protocols`` array, listing ``http``, ``https``, and also ``ftp``. .. configuration-block:: .. code-block:: php-annotations // src/AppBundle/Entity/Author.php namespace AppBundle\Entity; use Symfony\Component\Validator\Constraints as Assert; class Author { /** * @Assert\Url( * protocols = {"http", "https", "ftp"} * ) */ protected $bioUrl; } .. code-block:: yaml # src/AppBundle/Resources/config/validation.yml AppBundle\Entity\Author: properties: bioUrl: - Url: { protocols: [http, https, ftp] } .. code-block:: xml .. code-block:: php // src/AppBundle/Entity/Author.php namespace AppBundle\Entity; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints as Assert; class Author { public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('bioUrl', new Assert\Url(array( 'protocols' => array('http', 'https', 'ftp'), ))); } } .. include:: /reference/constraints/_payload-option.rst.inc checkDNS ~~~~~~~~ .. versionadded:: 2.7 The ``checkDNS`` option was introduced in Symfony 2.7. **type**: ``boolean`` **default**: ``false`` By default, this constraint just validates the syntax of the given URL. If you also need to check whether the associated host exists, set the ``checkDNS`` option to ``true``: .. configuration-block:: .. code-block:: php-annotations // src/AppBundle/Entity/Author.php namespace AppBundle\Entity; use Symfony\Component\Validator\Constraints as Assert; class Author { /** * @Assert\Url( * checkDNS = true * ) */ protected $bioUrl; } .. code-block:: yaml # src/AppBundle/Resources/config/validation.yml AppBundle\Entity\Author: properties: bioUrl: - Url: { checkDNS: true } .. code-block:: xml .. code-block:: php // src/AppBundle/Entity/Author.php namespace AppBundle\Entity; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints as Assert; class Author { public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('bioUrl', new Assert\Url(array( 'checkDNS' => true, ))); } } This option uses the :phpfunction:`checkdnsrr` PHP function to check the validity of the ``ANY`` DNS record corresponding to the host associated with the given URL. dnsMessage ~~~~~~~~~~ .. versionadded:: 2.7 The ``dnsMessage`` option was introduced in Symfony 2.7. **type**: ``string`` **default**: ``The host could not be resolved.`` This message is shown when the ``checkDNS`` option is set to ``true`` and the DNS check failed. .. configuration-block:: .. code-block:: php-annotations // src/AppBundle/Entity/Author.php namespace AppBundle\Entity; use Symfony\Component\Validator\Constraints as Assert; class Author { /** * @Assert\Url( * dnsMessage = "The host '{{ value }}' could not be resolved." * ) */ protected $bioUrl; } .. code-block:: yaml # src/AppBundle/Resources/config/validation.yml AppBundle\Entity\Author: properties: bioUrl: - Url: { dnsMessage: 'The host "{{ value }}" could not be resolved.' } .. code-block:: xml .. code-block:: php // src/AppBundle/Entity/Author.php namespace AppBundle\Entity; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints as Assert; class Author { public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('bioUrl', new Assert\Url(array( 'dnsMessage' => 'The host "{{ value }}" could not be resolved.', ))); } }