-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Validator] add documentation for the new Timezone
constraint.
#11317
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 2 commits
b0b1498
135c5ff
ac6282f
d564218
b37bc78
c5681bf
9cbeb75
ec7bd40
5ec9159
8e33d47
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
Timezone | ||
======== | ||
|
||
Validates that a value is a valid timezone identifier (e.g. ``Europe/Paris``). | ||
|
||
========== =================================================================== | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Applies to :ref:`property or method <validation-property-target>` | ||
Options - `groups`_ | ||
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. @symfony/team-symfony-docs do we use ordered options here and the same order for the explanations? 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. We didn't reorder descriptions for existing docs in the past and only sorted the table of contents to keep the diff maintainable. But here it makes sense to sort from the beginning. 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. Yes 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. What do you mean? Ordering options like:
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. @xabbuh although you are right ... for the Constraints docs we recently sorted both the list of options and their descriptions. It was a hell to fix all the conflicts, but we did it anyways :) |
||
- `message`_ | ||
- `payload`_ | ||
Class :class:`Symfony\\Component\\Validator\\Constraints\\Timezone` | ||
Validator :class:`Symfony\\Component\\Validator\\Constraints\\TimezoneValidator` | ||
========== =================================================================== | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Basic Usage | ||
----------- | ||
|
||
Suppose you have a ``UserSettings`` class, with a ``timezone`` field that is a | ||
string meant to contain a timezone identifier (ie. ``America/New_York``): | ||
javiereguiluz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Entity/UserSettings.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class UserSettings | ||
{ | ||
/** | ||
* @Assert\Timezone | ||
*/ | ||
protected $timezone; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# config/validator/validation.yaml | ||
App\Entity\UserSettings: | ||
properties: | ||
timezone: | ||
- Timezone: ~ | ||
|
||
.. 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\UserSettings"> | ||
<property name="timezone"> | ||
<constraint name="Timezone"/> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Entity/UserSettings.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Event | ||
hhamon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
protected $timezone; | ||
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. If I am correct, this should be removed as it is not added in other constraints |
||
|
||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('startsAt', new Assert\Timezone()); | ||
javiereguiluz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
.. 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 timezone.`` | ||
|
||
This message is shown if the underlying data is not a valid timezone identifier. | ||
|
||
You can use the following parameters in this message: | ||
|
||
=============== ============================================================== | ||
Parameter Description | ||
=============== ============================================================== | ||
``{{ value }}`` The current (invalid) value | ||
=============== ============================================================== | ||
|
||
.. include:: /reference/constraints/_payload-option.rst.inc | ||
|
||
zone | ||
~~~~ | ||
|
||
**type**: ``string`` **default**: ``\DateTimeZone::ALL`` | ||
|
||
The geographical zone in which to validate the timezone identifier. | ||
|
||
Value must be any of the `DateTimeZone`_ class constants values. | ||
|
||
countryCode | ||
hhamon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
~~~~~~~~~~~ | ||
|
||
**type**: ``string`` **default**: ``null`` | ||
|
||
This option must be used only when the ``zone`` option value equals ``\DateTimeZone::PER_COUNTRY``. | ||
|
||
The ``countryCode`` option enables to validate the timezone identifier is supported by the country code. | ||
javiereguiluz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Value must be a valid `ISO 3166-1 alpha-2`_ country code (e.g. ``BE``). | ||
|
||
.. _`DateTimeZone`: https://www.php.net/datetimezone | ||
.. _`ISO 3166-1 alpha-2`: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 |
Uh oh!
There was an error while loading. Please reload this page.