-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Validator] Documented the Hostname constraint #13167
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
Hostname | ||
======== | ||
|
||
This constraint ensures that the given value is a valid host name (internally it | ||
uses the ``FILTER_VALIDATE_DOMAIN`` option of the :phpfunction:`filter_var` PHP | ||
function). | ||
|
||
.. versionadded:: 5.1 | ||
|
||
The ``Hostname`` constraint was introduced in Symfony 5.1. | ||
|
||
========== =================================================================== | ||
Applies to :ref:`property or method <validation-property-target>` | ||
Options - `groups`_ | ||
- `message`_ | ||
- `payload`_ | ||
- `requireTld`_ | ||
Class :class:`Symfony\\Component\\Validator\\Constraints\\Hostname` | ||
Validator :class:`Symfony\\Component\\Validator\\Constraints\\HostnameValidator` | ||
========== =================================================================== | ||
|
||
Basic Usage | ||
----------- | ||
|
||
To use the Hostname validator, apply it to a property on an object that | ||
will contain a host name. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Entity/ServerSettings.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class ServerSettings | ||
{ | ||
/** | ||
* @Assert\Hostname(message="The server name must be a valid hostname.") | ||
*/ | ||
protected $name; | ||
javiereguiluz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# config/validator/validation.yaml | ||
App\Entity\ServerSettings: | ||
properties: | ||
name: | ||
- Hostname: | ||
message: The server name must be a valid hostname. | ||
|
||
.. code-block:: xml | ||
|
||
<!-- config/validator/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 https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
|
||
<class name="App\Entity\ServerSettings"> | ||
<property name="name"> | ||
<constraint name="Hostname"> | ||
<option name="message">The server name must be a valid hostname.</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Entity/ServerSettings.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
|
||
class ServerSettings | ||
{ | ||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('name', new Assert\Hostname([ | ||
'message' => 'The server name must be a valid hostname.', | ||
])); | ||
} | ||
} | ||
|
||
The following top-level domains (TLD) are reserved according to `RFC 2606`_ and | ||
that's why hostnames containing them are not considered valid: ``.example``, | ||
``.invalid``, ``.localhost``, and ``.test``. | ||
|
||
.. include:: /reference/constraints/_empty-values-are-valid.rst.inc | ||
|
||
Options | ||
------- | ||
|
||
.. include:: /reference/constraints/_groups-option.rst.inc | ||
|
||
``message`` | ||
~~~~~~~~~~~ | ||
|
||
**type**: ``string`` **default**: ``This value is not a valid hostname.`` | ||
|
||
The default message supplied when the value is not a valid hostname. | ||
|
||
You can use the following parameters in this message: | ||
|
||
=============== ============================================================== | ||
Parameter Description | ||
=============== ============================================================== | ||
``{{ value }}`` The current (invalid) value | ||
=============== ============================================================== | ||
|
||
.. include:: /reference/constraints/_payload-option.rst.inc | ||
|
||
``requireTld`` | ||
~~~~~~~~~~~~~~ | ||
|
||
**type**: ``bool`` **default**: ``true`` | ||
|
||
By default, hostnames are considered valid only when they are fully qualified | ||
and include their TLDs (top-level domain names). For instance, ``example.com`` | ||
is valid but ``example`` is not. | ||
|
||
Set this option to ``false`` to not require any TLD in the hostnames. | ||
|
||
.. note:: | ||
|
||
This constraint does not validate that the given TLD value is included in | ||
the `list of official top-level domains`_ (because that list is growing | ||
continuously and it's hard to keep track of it). | ||
|
||
.. _`RFC 2606`: https://tools.ietf.org/html/rfc2606 | ||
.. _`list of official top-level domains`: https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.