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

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add specific errors to be used when zone or countryCode are provided
  • Loading branch information
phansys committed Nov 29, 2018
commit 6be950a4d5b87f0fd9cccd522afa6e96be7dde9f
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
class Timezone extends Constraint
{
const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a';
const NO_SUCH_TIMEZONE_IN_ZONE_ERROR = 'b57767b1-36c0-40ac-a3d7-629420c775b8';
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ public function validate($value, Constraint $constraint)
$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 👍


if ($timezoneIds && !in_array($value, $timezoneIds, true)) {
if ($constraint->countryCode) {
$code = Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR;
} elseif (null !== $constraint->zone) {
$code = Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR;
} else {
$code = Timezone::NO_SUCH_TIMEZONE_ERROR;
}

$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?

->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
->setCode($code)
->addViolation();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function testInvalidGroupedTimezones($timezone, $what, $extraInfo)

$this->buildViolation('myMessage')
->setParameter('{{ extra_info }}', '"'.$extraInfo.'"')
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
->setCode(Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR)
->assertRaised();
}

Expand Down Expand Up @@ -207,7 +207,7 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country,

$this->buildViolation('myMessage')
->setParameter('{{ extra_info }}', '"'.$extraInfo.'"')
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
->setCode(Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR)
->assertRaised();
}

Expand Down