Skip to content

[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

Merged
merged 10 commits into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reference/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Validation Constraints Reference
constraints/Language
constraints/Locale
constraints/Country
constraints/Timezone

constraints/File
constraints/Image
Expand Down
144 changes: 144 additions & 0 deletions reference/constraints/Timezone.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
Timezone
========

Validates that a value is a valid timezone identifier (e.g. ``Europe/Paris``).

========== ======================================================================
Applies to :ref:`property or method <validation-property-target>`
Options - `groups`_
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Ordering options like:

  1. Child class options first then inherited options?
  2. The opposite order of 1.?

Copy link
Member

Choose a reason for hiding this comment

The 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`_
- `zone`_
- `countryCode`_
Class :class:`Symfony\\Component\\Validator\\Constraints\\Timezone`
Validator :class:`Symfony\\Component\\Validator\\Constraints\\TimezoneValidator`
========== ======================================================================

Basic Usage
-----------

Suppose you have a ``UserSettings`` class, with a ``timezone`` field that is a
string meant to contain a timezone identifier (e.g. ``America/New_York``):

.. 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 UserSettings
{
protected $timezone;
Copy link
Contributor

Choose a reason for hiding this comment

The 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('timezone', new Assert\Timezone());
}
}

.. 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``

Set this option to any of the following constants to restrict the valid timezone
identifiers to the ones that belong to that geographical zone:

* ``\DateTimeZone::AFRICA``
* ``\DateTimeZone::AMERICA``
* ``\DateTimeZone::ANTARCTICA``
* ``\DateTimeZone::ARCTIC``
* ``\DateTimeZone::ASIA``
* ``\DateTimeZone::ATLANTIC``
* ``\DateTimeZone::AUSTRALIA``
* ``\DateTimeZone::EUROPE``
* ``\DateTimeZone::INDIAN``
* ``\DateTimeZone::PACIFIC``

The special ``\DateTimeZone::ALL`` zone accepts any timezone excluding deprecated timezones.

The special ``\DateTimeZone::ALL_WITH_BC`` zone accepts any timezone including deprecated timezones.

The special ``\DateTimeZone::PER_COUNTRY`` zone limits the timezones to a certain country. This zone
value must be used in combination with the ``countryCode`` option.

countryCode
~~~~~~~~~~~

**type**: ``string`` **default**: ``null``

If the ``zone`` option is set to ``\DateTimeZone::PER_COUNTRY``, this option
restricts the valid timezone identifiers to the ones that belong to the given
country.

The value of this option must be a valid `ISO 3166-1 alpha-2`_ country code
(e.g. ``CN`` for China).

.. _`DateTimeZone`: https://www.php.net/datetimezone
.. _`ISO 3166-1 alpha-2`: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
1 change: 1 addition & 0 deletions reference/constraints/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Choice Constraints
* :doc:`Language </reference/constraints/Language>`
* :doc:`Locale </reference/constraints/Locale>`
* :doc:`Country </reference/constraints/Country>`
* :doc:`Timezone </reference/constraints/Timezone>`

File Constraints
~~~~~~~~~~~~~~~~
Expand Down