Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
wants to merge 14 commits into from
Closed

Conversation

phansys
Copy link
Contributor

@phansys phansys commented Apr 4, 2017

Q A
Branch master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a (yet)

Add TimezoneValidator in order to validate timezones against \DateTimeZone::listIdentifiers().

TODO:

  • Add support for constraints with \DateTimeZone::PER_COUNTRY argument
  • Improve the validation message

@phansys phansys changed the title Add TimeZoneValidator [Validator] Add TimeZoneValidator Apr 4, 2017
@phansys phansys changed the title [Validator] Add TimeZoneValidator [Validator] [WIP] Add TimeZoneValidator Apr 4, 2017
@nicolas-grekas
Copy link
Member

should be with a lowercase "z", see #21875

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Apr 4, 2017
@phansys phansys force-pushed the tz_validator branch 2 times, most recently from dbe965b to 0328eb0 Compare April 4, 2017 16:08
@phansys
Copy link
Contributor Author

phansys commented Apr 4, 2017

Simple question: since DateTimeZone::listIdentifiers() accepts a string with "a two-letter ISO 3166-1 compatible country code" as second argument, could we use Country validator at Timezone constructor in order to validate the countryCode argument?

@phansys phansys changed the title [Validator] [WIP] Add TimeZoneValidator [Validator] [WIP] Add TimezoneValidator Apr 4, 2017
@phansys phansys changed the title [Validator] [WIP] Add TimezoneValidator [Validator] Add TimezoneValidator Apr 4, 2017
@phansys phansys force-pushed the tz_validator branch 2 times, most recently from bbc03de to e5b599f Compare April 9, 2017 20:21
}

$this->context->buildViolation($constraint->message)
->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->zone, $constraint->countryCode))
Copy link
Contributor

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'])) {
Copy link
Contributor

@ro0NL ro0NL Jul 14, 2017

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',
Copy link
Contributor

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;

Copy link
Contributor

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);
Copy link
Contributor

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 👍

@nicolas-grekas
Copy link
Member

Moving to 4.1. Rebase on master might be needed, where PHP 7.1 features can be used btw.

@nicolas-grekas nicolas-grekas modified the milestones: 3.4, 4.1 Oct 8, 2017
@phansys phansys force-pushed the tz_validator branch 2 times, most recently from 58de37e to b8cce71 Compare February 6, 2018 17:50
@phansys
Copy link
Contributor Author

phansys commented Feb 6, 2018

Rebased. I'll be waiting for feedback about the @ro0NL's proposal to use DateTimeZone::ALL as default zone. The other comments were already attended.

@phansys phansys force-pushed the tz_validator branch 2 times, most recently from 386a46c to 72505af Compare February 26, 2018 03:03
@nicolas-grekas nicolas-grekas modified the milestones: 4.1, next Apr 20, 2018
*/
public function __construct(array $options = null)
{
$this->zone = $options['zone'] ?? null;
Copy link
Contributor

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
Copy link
Contributor

@ro0NL ro0NL Sep 30, 2018

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 }}.';
Copy link
Contributor

@ro0NL ro0NL Sep 30, 2018

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

Copy link
Contributor

@ro0NL ro0NL left a 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.

@hhamon
Copy link
Contributor

hhamon commented Apr 6, 2019

Hey!

What's the status of this PR please? Can I try to finish it?

@hhamon
Copy link
Contributor

hhamon commented Apr 6, 2019

This PR has been reworked here #30900.

@fabpot
Copy link
Member

fabpot commented Apr 6, 2019

Closing in favor of #30900.

@fabpot fabpot closed this Apr 6, 2019
@phansys phansys deleted the tz_validator branch April 6, 2019 17:57
fabpot added a commit that referenced this pull request Apr 6, 2019
…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.
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants