Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b8cce71

Browse filesBrowse files
committed
Update validation message
1 parent daa5605 commit b8cce71
Copy full SHA for b8cce71

File tree

3 files changed

+76
-58
lines changed
Filter options

3 files changed

+76
-58
lines changed

‎src/Symfony/Component/Validator/Constraints/Timezone.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Timezone.php
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,21 @@ class Timezone extends Constraint
2727
const NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b';
2828

2929
public $zone;
30-
3130
public $countryCode;
32-
33-
public $message = 'This value is not a valid timezone{{ extra_info }}.';
31+
public $message = 'This value is not a valid timezone{{ zone_message }}{{ country_code_message }}.';
3432

3533
protected static $errorNames = array(
3634
self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR',
35+
self::NO_SUCH_TIMEZONE_IN_ZONE_ERROR => 'NO_SUCH_TIMEZONE_IN_ZONE_ERROR',
36+
self::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR => 'NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR',
3737
);
3838

3939
/**
4040
* {@inheritdoc}
4141
*/
42-
public function __construct($options = null)
42+
public function __construct(array $options = null)
4343
{
44-
if (isset($options['zone'])) {
45-
$this->zone = $options['zone'];
46-
}
44+
$this->zone = $options['zone'] ?? null;
4745

4846
if (isset($options['countryCode'])) {
4947
if (\DateTimeZone::PER_COUNTRY !== $this->zone) {

‎src/Symfony/Component/Validator/Constraints/TimezoneValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/TimezoneValidator.php
+37-21Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ public function validate($value, Constraint $constraint)
4141

4242
$value = (string) $value;
4343
$zone = null !== $constraint->zone ? $constraint->zone : \DateTimeZone::ALL;
44-
$timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode);
44+
45+
// @see: https://bugs.php.net/bug.php?id=75928
46+
if ($constraint->countryCode) {
47+
$timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode);
48+
} else {
49+
$timezoneIds = \DateTimeZone::listIdentifiers($zone);
50+
}
4551

4652
if ($timezoneIds && !in_array($value, $timezoneIds, true)) {
4753
if ($constraint->countryCode) {
@@ -52,10 +58,16 @@ public function validate($value, Constraint $constraint)
5258
$code = Timezone::NO_SUCH_TIMEZONE_ERROR;
5359
}
5460

55-
$this->context->buildViolation($constraint->message)
56-
->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->zone, $constraint->countryCode))
61+
$violation = $this->context->buildViolation($constraint->message);
62+
63+
foreach ($this->generateValidationMessage($constraint->zone, $constraint->countryCode) as $placeholder => $message) {
64+
$violation->setParameter($placeholder, $message);
65+
}
66+
67+
$violation
5768
->setCode($code)
58-
->addViolation();
69+
->addViolation()
70+
;
5971
}
6072
}
6173

@@ -68,31 +80,35 @@ public function getDefaultOption()
6880
}
6981

7082
/**
71-
* Format the extra info which is appended to validation message based on
72-
* constraint options.
83+
* Generates the replace parameters which are used in validation message.
7384
*
7485
* @param int|null $zone
7586
* @param string|null $countryCode
7687
*
77-
* @return string
88+
* @return array
7889
*/
79-
private function formatExtraInfo($zone, $countryCode = null)
90+
private function generateValidationMessage(int $zone = null, string $countryCode = null): array
8091
{
81-
if (null === $zone) {
82-
return '';
83-
}
84-
if ($countryCode) {
85-
$value = ' for ISO 3166-1 country code "'.$countryCode.'"';
86-
} else {
87-
$r = new \ReflectionClass('\DateTimeZone');
88-
$consts = $r->getConstants();
89-
if ($value = array_search($zone, $consts, true)) {
90-
$value = ' for "'.$value.'" zone';
91-
} else {
92-
$value = ' for zone with identifier '.$zone;
92+
$values = array(
93+
'{{ country_code_message }}' => '',
94+
'{{ zone_message }}' => '',
95+
);
96+
97+
if (null !== $zone) {
98+
if (\DateTimeZone::PER_COUNTRY !== $zone) {
99+
$r = new \ReflectionClass(\DateTimeZone::class);
100+
$consts = $r->getConstants();
101+
if ($zoneFound = array_search($zone, $consts, true)) {
102+
$values['{{ zone_message }}'] = ' at "'.$zoneFound.'" zone';
103+
} else {
104+
$values['{{ zone_message }}'] = ' at zone with identifier '.$zone;
105+
}
106+
}
107+
if ($countryCode) {
108+
$values['{{ country_code_message }}'] = ' for ISO 3166-1 country code "'.$countryCode.'"';
93109
}
94110
}
95111

96-
return $value;
112+
return $values;
97113
}
98114
}

‎src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php
+34-30Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class TimezoneValidatorTest extends ConstraintValidatorTestCase
2222
{
23-
protected function createValidator()
23+
protected function createValidator(): TimezoneValidator
2424
{
2525
return new TimezoneValidator();
2626
}
@@ -50,14 +50,14 @@ public function testExpectsStringCompatibleType()
5050
/**
5151
* @dataProvider getValidTimezones
5252
*/
53-
public function testValidTimezones($timezone)
53+
public function testValidTimezones(string $timezone)
5454
{
5555
$this->validator->validate($timezone, new Timezone());
5656

5757
$this->assertNoViolation();
5858
}
5959

60-
public function getValidTimezones()
60+
public function getValidTimezones(): iterable
6161
{
6262
return array(
6363
array('America/Argentina/Buenos_Aires'),
@@ -73,7 +73,7 @@ public function getValidTimezones()
7373
/**
7474
* @dataProvider getValidGroupedTimezones
7575
*/
76-
public function testValidGroupedTimezones($timezone, $what)
76+
public function testValidGroupedTimezones(string $timezone, int $what)
7777
{
7878
$constraint = new Timezone(array(
7979
'zone' => $what,
@@ -84,7 +84,7 @@ public function testValidGroupedTimezones($timezone, $what)
8484
$this->assertNoViolation();
8585
}
8686

87-
public function getValidGroupedTimezones()
87+
public function getValidGroupedTimezones(): iterable
8888
{
8989
return array(
9090
array('America/Argentina/Cordoba', \DateTimeZone::AMERICA),
@@ -106,7 +106,7 @@ public function getValidGroupedTimezones()
106106
/**
107107
* @dataProvider getInvalidTimezones
108108
*/
109-
public function testInvalidTimezonesWithoutZone($timezone, $extraInfo)
109+
public function testInvalidTimezonesWithoutZone(string $timezone, string $zoneMessage, string $countryCodeMessage)
110110
{
111111
$constraint = new Timezone(array(
112112
'message' => 'myMessage',
@@ -115,24 +115,25 @@ public function testInvalidTimezonesWithoutZone($timezone, $extraInfo)
115115
$this->validator->validate($timezone, $constraint);
116116

117117
$this->buildViolation('myMessage')
118-
->setParameter('{{ extra_info }}', $extraInfo)
118+
->setParameter('{{ zone_message }}', $zoneMessage)
119+
->setParameter('{{ country_code_message }}', $countryCodeMessage)
119120
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
120121
->assertRaised();
121122
}
122123

123-
public function getInvalidTimezones()
124+
public function getInvalidTimezones(): iterable
124125
{
125126
return array(
126-
array('Buenos_Aires/Argentina/America', ''),
127-
array('Mayotte/Indian', ''),
128-
array('foobar', ''),
127+
array('Buenos_Aires/Argentina/America', '', ''),
128+
array('Mayotte/Indian', '', ''),
129+
array('foobar', '', ''),
129130
);
130131
}
131132

132133
/**
133134
* @dataProvider getInvalidGroupedTimezones
134135
*/
135-
public function testInvalidGroupedTimezones($timezone, $what, $extraInfo)
136+
public function testInvalidGroupedTimezones(string $timezone, int $what, string $zoneMessage, string $countryCodeMessage)
136137
{
137138
$constraint = new Timezone(array(
138139
'zone' => $what,
@@ -142,26 +143,27 @@ public function testInvalidGroupedTimezones($timezone, $what, $extraInfo)
142143
$this->validator->validate($timezone, $constraint);
143144

144145
$this->buildViolation('myMessage')
145-
->setParameter('{{ extra_info }}', $extraInfo)
146+
->setParameter('{{ zone_message }}', $zoneMessage)
147+
->setParameter('{{ country_code_message }}', $countryCodeMessage)
146148
->setCode(Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR)
147149
->assertRaised();
148150
}
149151

150-
public function getInvalidGroupedTimezones()
152+
public function getInvalidGroupedTimezones(): iterable
151153
{
152154
return array(
153-
array('Antarctica/McMurdo', \DateTimeZone::AMERICA, ' for "AMERICA" zone'),
154-
array('America/Barbados', \DateTimeZone::ANTARCTICA, ' for "ANTARCTICA" zone'),
155-
array('Europe/Kiev', \DateTimeZone::ARCTIC, ' for "ARCTIC" zone'),
156-
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN, ' for "INDIAN" zone'),
157-
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA, ' for zone with identifier 260'),
155+
array('Antarctica/McMurdo', \DateTimeZone::AMERICA, ' at "AMERICA" zone', ''),
156+
array('America/Barbados', \DateTimeZone::ANTARCTICA, ' at "ANTARCTICA" zone', ''),
157+
array('Europe/Kiev', \DateTimeZone::ARCTIC, ' at "ARCTIC" zone', ''),
158+
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN, ' at "INDIAN" zone', ''),
159+
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA, ' at zone with identifier 260', ''),
158160
);
159161
}
160162

161163
/**
162164
* @dataProvider getValidGroupedTimezonesByCountry
163165
*/
164-
public function testValidGroupedTimezonesByCountry($timezone, $what, $country)
166+
public function testValidGroupedTimezonesByCountry(string $timezone, int $what, string $country)
165167
{
166168
$constraint = new Timezone(array(
167169
'zone' => $what,
@@ -173,7 +175,7 @@ public function testValidGroupedTimezonesByCountry($timezone, $what, $country)
173175
$this->assertNoViolation();
174176
}
175177

176-
public function getValidGroupedTimezonesByCountry()
178+
public function getValidGroupedTimezonesByCountry(): iterable
177179
{
178180
return array(
179181
array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'AR'),
@@ -195,7 +197,7 @@ public function getValidGroupedTimezonesByCountry()
195197
/**
196198
* @dataProvider getInvalidGroupedTimezonesByCountry
197199
*/
198-
public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country, $extraInfo)
200+
public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what, string $country, string $zoneMessage, string $countryCodeMessage)
199201
{
200202
$constraint = new Timezone(array(
201203
'message' => 'myMessage',
@@ -206,23 +208,24 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country,
206208
$this->validator->validate($timezone, $constraint);
207209

208210
$this->buildViolation('myMessage')
209-
->setParameter('{{ extra_info }}', $extraInfo)
211+
->setParameter('{{ zone_message }}', $zoneMessage)
212+
->setParameter('{{ country_code_message }}', $countryCodeMessage)
210213
->setCode(Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR)
211214
->assertRaised();
212215
}
213216

214-
public function getInvalidGroupedTimezonesByCountry()
217+
public function getInvalidGroupedTimezonesByCountry(): iterable
215218
{
216219
return array(
217-
array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR', ' for ISO 3166-1 country code "FR"'),
218-
array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', ' for ISO 3166-1 country code "PT"'),
220+
array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR', '', ' for ISO 3166-1 country code "FR"'),
221+
array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', '', ' for ISO 3166-1 country code "PT"'),
219222
);
220223
}
221224

222225
/**
223226
* @dataProvider getDeprecatedTimezones
224227
*/
225-
public function testDeprecatedTimezonesAreVaildWithBC($timezone)
228+
public function testDeprecatedTimezonesAreVaildWithBC(string $timezone)
226229
{
227230
$constraint = new Timezone(array(
228231
'zone' => \DateTimeZone::ALL_WITH_BC,
@@ -236,7 +239,7 @@ public function testDeprecatedTimezonesAreVaildWithBC($timezone)
236239
/**
237240
* @dataProvider getDeprecatedTimezones
238241
*/
239-
public function testDeprecatedTimezonesAreInvaildWithoutBC($timezone)
242+
public function testDeprecatedTimezonesAreInvaildWithoutBC(string $timezone)
240243
{
241244
$constraint = new Timezone(array(
242245
'message' => 'myMessage',
@@ -245,12 +248,13 @@ public function testDeprecatedTimezonesAreInvaildWithoutBC($timezone)
245248
$this->validator->validate($timezone, $constraint);
246249

247250
$this->buildViolation('myMessage')
248-
->setParameter('{{ extra_info }}', '')
251+
->setParameter('{{ zone_message }}', '')
252+
->setParameter('{{ country_code_message }}', '')
249253
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
250254
->assertRaised();
251255
}
252256

253-
public function getDeprecatedTimezones()
257+
public function getDeprecatedTimezones(): iterable
254258
{
255259
return array(
256260
array('America/Buenos_Aires'),

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.