-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Validator] Add TimezoneValidator
#22262
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 1 commit
4b87fb7
6cceda0
6124ef6
49e4b51
288a413
7545275
de78802
dfa4f98
6be950a
6d23035
13bc629
0491875
14689f7
c3dce5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,23 +27,21 @@ class Timezone extends Constraint | |
| const NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b'; | ||
|
|
||
| public $zone; | ||
|
|
||
| public $countryCode; | ||
|
|
||
| public $message = 'This value is not a valid timezone{{ extra_info }}.'; | ||
| public $message = 'This value is not a valid timezone{{ zone_message }}{{ country_code_message }}.'; | ||
|
|
||
| protected static $errorNames = array( | ||
| self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR', | ||
|
Contributor
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. missing few codes here |
||
| self::NO_SUCH_TIMEZONE_IN_ZONE_ERROR => 'NO_SUCH_TIMEZONE_IN_ZONE_ERROR', | ||
| self::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR => 'NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR', | ||
| ); | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function __construct($options = null) | ||
| public function __construct(array $options = null) | ||
| { | ||
| if (isset($options['zone'])) { | ||
| $this->zone = $options['zone']; | ||
| } | ||
| $this->zone = $options['zone'] ?? null; | ||
|
||
|
|
||
| if (isset($options['countryCode'])) { | ||
| if (\DateTimeZone::PER_COUNTRY !== $this->zone) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,13 @@ public function validate($value, Constraint $constraint) | |
|
|
||
| $value = (string) $value; | ||
| $zone = null !== $constraint->zone ? $constraint->zone : \DateTimeZone::ALL; | ||
| $timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode); | ||
|
|
||
| // @see: https://bugs.php.net/bug.php?id=75928 | ||
| if ($constraint->countryCode) { | ||
| $timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode); | ||
| } else { | ||
| $timezoneIds = \DateTimeZone::listIdentifiers($zone); | ||
| } | ||
|
|
||
| if ($timezoneIds && !in_array($value, $timezoneIds, true)) { | ||
| if ($constraint->countryCode) { | ||
|
|
@@ -52,10 +58,16 @@ public function validate($value, Constraint $constraint) | |
| $code = Timezone::NO_SUCH_TIMEZONE_ERROR; | ||
| } | ||
|
|
||
| $this->context->buildViolation($constraint->message) | ||
| ->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->zone, $constraint->countryCode)) | ||
| $violation = $this->context->buildViolation($constraint->message); | ||
|
|
||
| foreach ($this->generateValidationMessage($constraint->zone, $constraint->countryCode) as $placeholder => $message) { | ||
| $violation->setParameter($placeholder, $message); | ||
| } | ||
|
|
||
| $violation | ||
| ->setCode($code) | ||
| ->addViolation(); | ||
| ->addViolation() | ||
| ; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -68,31 +80,35 @@ public function getDefaultOption() | |
| } | ||
|
|
||
| /** | ||
| * Format the extra info which is appended to validation message based on | ||
| * constraint options. | ||
| * Generates the replace parameters which are used in validation message. | ||
| * | ||
| * @param int|null $zone | ||
| * @param string|null $countryCode | ||
| * | ||
| * @return string | ||
| * @return array | ||
| */ | ||
| private function formatExtraInfo($zone, $countryCode = null) | ||
| private function generateValidationMessage(int $zone = null, string $countryCode = null): array | ||
|
||
| { | ||
| if (null === $zone) { | ||
| return ''; | ||
| } | ||
| if ($countryCode) { | ||
| $value = ' for ISO 3166-1 country code "'.$countryCode.'"'; | ||
| } else { | ||
| $r = new \ReflectionClass('\DateTimeZone'); | ||
| $consts = $r->getConstants(); | ||
| if ($value = array_search($zone, $consts, true)) { | ||
| $value = ' for "'.$value.'" zone'; | ||
| } else { | ||
| $value = ' for zone with identifier '.$zone; | ||
| $values = array( | ||
| '{{ country_code_message }}' => '', | ||
| '{{ zone_message }}' => '', | ||
| ); | ||
|
|
||
| if (null !== $zone) { | ||
| if (\DateTimeZone::PER_COUNTRY !== $zone) { | ||
| $r = new \ReflectionClass(\DateTimeZone::class); | ||
| $consts = $r->getConstants(); | ||
| if ($zoneFound = array_search($zone, $consts, true)) { | ||
| $values['{{ zone_message }}'] = ' at "'.$zoneFound.'" zone'; | ||
| } else { | ||
| $values['{{ zone_message }}'] = ' at zone with identifier '.$zone; | ||
| } | ||
| } | ||
| if ($countryCode) { | ||
| $values['{{ country_code_message }}'] = ' for ISO 3166-1 country code "'.$countryCode.'"'; | ||
| } | ||
| } | ||
|
|
||
| return $value; | ||
| return $values; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd say
This value is not a valid timezone.the standard seems to not include the value in the message (which in this case cannot be translated), see e.g. https://github.com/symfony/symfony/blob/v4.1.5/src/Symfony/Component/Validator/Constraints/Currency.php#L31
also the message should be added too
validators.en.xml+ any language you know