8000 Add specific errors to be used when `zone` or `countryCode` are provided · symfony/symfony@bbc03de · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit bbc03de

Browse files
committed
Add specific errors to be used when zone or countryCode are provided
1 parent e09ad49 commit bbc03de

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
class Timezone extends Constraint
2424
{
2525
const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a';
26+
const NO_SUCH_TIMEZONE_IN_ZONE_ERROR = 'b57767b1-36c0-40ac-a3d7-629420c775b8';
27+
const NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b';
2628

2729
public $zone;
2830

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ public function validate($value, Constraint $constraint)
4444
$timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode);
4545

4646
if ($timezoneIds && !in_array($value, $timezoneIds, true)) {
47+
if ($constraint->countryCode) {
48+
$code = Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR;
49+
} elseif (null !== $constraint->zone) {
50+
$code = Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR;
51+
} else {
52+
$code = Timezone::NO_SUCH_TIMEZONE_ERROR;
53+
}
54+
4755
$this->context->buildViolation($constraint->message)
4856
->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->zone, $constraint->countryCode))
49-
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
57+
->setCode($code)
5058
->addViolation();
5159
}
5260
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function testInvalidGroupedTimezones($timezone, $what, $extraInfo)
143143

144144
$this->buildViolation('myMessage')
145145
->setParameter('{{ extra_info }}', '"'.$extraInfo.'"')
146-
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
146+
->setCode(Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR)
147147
->assertRaised();
148148
}
149149

@@ -207,7 +207,7 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country,
207207

208208
$this->buildViolation('myMessage')
209209
->setParameter('{{ extra_info }}', '"'.$extraInfo.'"')
210-
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
210+
->setCode(Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR)
211211
->assertRaised();
212212
}
213213

0 commit comments

Comments
 (0)
0