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 1 commit
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
2 changes: 2 additions & 0 deletions reference/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Validation Constraints Reference
constraints/DateTime
constraints/Time

constraints/Timezone

constraints/Choice
constraints/Collection
constraints/Count
Expand Down
127 changes: 127 additions & 0 deletions reference/constraints/Timezone.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
Timezone
========

Validates that a value is a valid timezone identifier (ie. ``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`_
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 (ie. `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
* @var string A timezone identifier
*/
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
{
/**
* @var string A timezone identifier
*/
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('startsAt', 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 time.``

This message is shown if the underlying data is not a valid time.

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
~~~~~~~~~~~

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

Value must be a valid `ISO 3166-1 alpha-2` country code (ie. `BE`).

.. _DateTimeZone: https://www.php.net/datetimezone