Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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 2 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
2 changes: 2 additions & 0 deletions 2 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
OskarStark marked this conversation as resolved.
Show resolved Hide resolved

constraints/Choice
constraints/Collection
constraints/Count
Expand Down
124 changes: 124 additions & 0 deletions 124 reference/constraints/Timezone.rst
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`_
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`
========== ===================================================================
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;
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());
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.