-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Documented the checkDNS option of the Url validator #5426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
75da2f3
bb3dd4e
1d3b192
a2fd9ef
5bcd48d
a04b4ac
2becf60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ Validates that a value is a valid URL string. | |
| Options | - `message`_ | | ||
| | - `protocols`_ | | ||
| | - `payload`_ | | ||
| | - `checkDNS`_ | | ||
+----------------+---------------------------------------------------------------------+ | ||
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Url` | | ||
+----------------+---------------------------------------------------------------------+ | ||
|
@@ -20,6 +21,14 @@ Basic Usage | |
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# src/Acme/BlogBundle/Resources/config/validation.yml | ||
Acme\BlogBundle\Entity\Author: | ||
properties: | ||
bioUrl: | ||
- Url: ~ | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Acme/BlogBundle/Entity/Author.php | ||
|
@@ -35,14 +44,6 @@ Basic Usage | |
protected $bioUrl; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# src/Acme/BlogBundle/Resources/config/validation.yml | ||
Acme\BlogBundle\Entity\Author: | ||
properties: | ||
bioUrl: | ||
- Url: ~ | ||
|
||
.. code-block:: xml | ||
|
||
<!-- src/Acme/BlogBundle/Resources/config/validation.xml --> | ||
|
@@ -84,13 +85,229 @@ 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 | ||
|
||
<!-- src/Acme/BlogBundle/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="bioUrl"> | ||
<constraint name="Url"> | ||
<option name="message">The url "{{ value }}" is not a valid url.</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. 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: ~ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here (and I prefer |
||
protocols: [http, https, ftp] | ||
|
||
.. code-block:: xml | ||
|
||
<!-- src/Acme/BlogBundle/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="bioUrl"> | ||
<constraint name="Url"> | ||
<option name="protocols"> | ||
<value>http</value> | ||
<value>https</value> | ||
<value>ftp</value> | ||
</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. 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'), | ||
))); | ||
} | ||
} | ||
|
||
.. include:: /reference/constraints/_payload-option.rst.inc | ||
|
||
checkDNS | ||
~~~~~~~~ | ||
|
||
**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/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", | ||
* protocols = {"http", "https"} | ||
* checkDNS = true | ||
* ) | ||
*/ | ||
protected $bioUrl; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# src/Acme/BlogBundle/Resources/config/validation.yml | ||
Acme\BlogBundle\Entity\Author: | ||
properties: | ||
bioUrl: | ||
- Url: ~ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
message: The url "{{ value }}" is not a valid url. | ||
protocols: [http, https] | ||
checkDNS: true | ||
|
||
.. code-block:: xml | ||
|
||
<!-- src/Acme/BlogBundle/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="bioUrl"> | ||
<constraint name="Url"> | ||
<option name="message">The url "{{ value }}" is not a valid url.</option> | ||
<option name="protocols"> | ||
<value>http</value> | ||
<value>https</value> | ||
</option> | ||
<option name="checkDNS">true</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. 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' => array('http', 'https'), | ||
'checkDNS' => true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please align |
||
))); | ||
} | ||
} | ||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...and here :)