diff --git a/reference/constraints/Url.rst b/reference/constraints/Url.rst index 26715d55c02..2a4cace9c86 100644 --- a/reference/constraints/Url.rst +++ b/reference/constraints/Url.rst @@ -83,11 +83,140 @@ message This message is shown if the URL is invalid. +.. configuration-block:: + + .. code-block:: php-annotations + + // src/Acme/BlogBundle/Entity/Author.php + namespace Acme\BlogBundle\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/Acme/BlogBundle/Resources/config/validation.yml + Acme\BlogBundle\Entity\Author: + properties: + bioUrl: + - Url: + message: The url "{{ value }}" is not a valid url. + + .. code-block:: xml + + + + + + + + + + + + + + + .. code-block:: 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('bioUrl', new Assert\Url(array( + 'message' => 'The url "{{ value }}" is not a valid url.', + ))); + } + } + protocols ~~~~~~~~~ **type**: ``array`` **default**: ``array('http', 'https')`` -The protocols that will be considered to be valid. For example, if you also -needed ``ftp://`` type URLs to be valid, you'd redefine the ``protocols`` -array, listing ``http``, ``https`` and also ``ftp``. +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/Acme/BlogBundle/Entity/Author.php + namespace Acme\BlogBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + /** + * @Assert\Url( + * protocols = {"http", "https", "ftp"} + * ) + */ + protected $bioUrl; + } + + .. code-block:: yaml + + # src/Acme/BlogBundle/Resources/config/validation.yml + Acme\BlogBundle\Entity\Author: + properties: + bioUrl: + - Url: { protocols: [http, https, ftp] } + + .. code-block:: xml + + + + + + + + + + + + + + + .. code-block:: 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('bioUrl', new Assert\Url(array( + 'protocols' => array('http', 'https', 'ftp'), + ))); + } + }