[Validator] Add TimezoneValidator#22262
Conversation
TimeZoneValidatorTimeZoneValidator
|
should be with a lowercase "z", see #21875 |
dbe965b to
0328eb0
Compare
|
Simple question: since |
TimeZoneValidatorTimezoneValidator
TimezoneValidatorTimezoneValidator
bbc03de to
e5b599f
Compare
| } | ||
|
|
||
| $this->context->buildViolation($constraint->message) | ||
| ->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->zone, $constraint->countryCode)) |
There was a problem hiding this comment.
This is a weird parameter, and binds localized text to this constraint validator... 👎 for that.
What about specialized params instead like zone_id / zone_name or so?
| */ | ||
| public function __construct($options = null) | ||
| { | ||
| if (isset($options['zone'])) { |
There was a problem hiding this comment.
What about \DateTimeZone::ALL as a default, or, if countryCode is given default to \DateTimeZone::PER_COUNTRY instead. So we dont have to expect null :)
| public $message = 'This value is not a valid timezone{{ extra_info }}.'; | ||
|
|
||
| protected static $errorNames = array( | ||
| self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR', |
| const NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b'; | ||
|
|
||
| public $zone; | ||
|
|
There was a problem hiding this comment.
newlines between public props can be removed
|
|
||
| $value = (string) $value; | ||
| $zone = null !== $constraint->zone ? $constraint->zone : \DateTimeZone::ALL; | ||
| $timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode); |
There was a problem hiding this comment.
listIdentifiers actually allows zones like \DateTimeZone::AMERICA | \DateTimeZone::EUROPE.. thats cool 👍
|
Moving to 4.1. Rebase on master might be needed, where PHP 7.1 features can be used btw. |
58de37e to
b8cce71
Compare
|
Rebased. I'll be waiting for feedback about the @ro0NL's proposal to use |
386a46c to
72505af
Compare
| */ | ||
| public function __construct(array $options = null) | ||
| { | ||
| $this->zone = $options['zone'] ?? null; |
There was a problem hiding this comment.
calling the parent constructor actually sets the options on properties, so i think we should do the validation step after based on the property values set.
| * | ||
| * @return array | ||
| */ | ||
| private function generateValidationMessage(int $zone = null, string $countryCode = null): array |
There was a problem hiding this comment.
i'd prefer not to have this method, the generated messages are also not translator friendly. Instead i would always set the (standard) {{ value }} parameter (being the zone ID in this case) see e.g. https://github.com/symfony/symfony/blob/v4.1.5/src/Symfony/Component/Validator/Constraints/CurrencyValidator.php#L48
|
|
||
| public $zone; | ||
| public $countryCode; | ||
| public $message = 'This value is not a valid timezone{{ zone_message }}{{ country_code_message }}.'; |
There was a problem hiding this comment.
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
72505af to
e1831c4
Compare
e1831c4 to
14689f7
Compare
|
Hey! What's the status of this PR please? Can I try to finish it? |
|
This PR has been reworked here #30900. |
|
Closing in favor of #30900. |
…phansys) This PR was merged into the 4.3-dev branch. Discussion ---------- [Validator] add new `Timezone` validation constraint | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT | Doc PR | symfony/symfony-docs#11317 Rework of #22262. Commits ------- 536e53f [Validator] add new `Timezone` validation constraint.
Add
TimezoneValidatorin order to validate timezones against\DateTimeZone::listIdentifiers().TODO:
\DateTimeZone::PER_COUNTRYargument