-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
TimeZoneValidator
TimeZoneValidator
should be with a lowercase "z", see #21875 |
dbe965b
to
0328eb0
Compare
Simple question: since |
TimeZoneValidator
TimezoneValidator
TimezoneValidator
TimezoneValidator
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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', |
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.
missing few codes here
const NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b'; | ||
|
||
public $zone; | ||
|
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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
72505af
to
e1831c4
Compare
e1831c4
to
14689f7
Compare
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.
see also #28831, as we have to decide wether we use PHP or Intl for validating.
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
TimezoneValidator
in order to validate timezones against\DateTimeZone::listIdentifiers()
.TODO:
\DateTimeZone::PER_COUNTRY
argument