Validates that a value is a valid timezone identifier (e.g. Europe/Paris
).
Applies to | property or method <validation-property-target> |
Options |
|
Class | Symfony\\Component\\Validator\\Constraints\\Timezone |
Validator | Symfony\\Component\\Validator\\Constraints\\TimezoneValidator |
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
):
// src/Entity/UserSettings.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class UserSettings
{
/**
* @Assert\Timezone
*/
protected $timezone;
}
# config/validator/validation.yaml
App\Entity\UserSettings:
properties:
timezone:
- Timezone: ~
<!-- 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>
// src/Entity/UserSettings.php
namespace App\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
class UserSettings
{
protected $timezone;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('timezone', new Assert\Timezone());
}
}
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 |
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 country_code
option.
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).