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 4f08f20

Browse filesBrowse files
committed
Deprecate use of Locale validation constraint without setting "canonicalize" option to true
1 parent 1364089 commit 4f08f20
Copy full SHA for 4f08f20

File tree

4 files changed

+103
-13
lines changed
Filter options

4 files changed

+103
-13
lines changed

‎src/Symfony/Component/Validator/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Deprecated the `checkDNS` and `dnsMessage` options of the `Url` constraint. They will be removed in 5.0.
88
* added a `values` option to the `Expression` constraint
9+
* Deprecated use of `Locale` constraint without setting `true` at "canonicalize" option, which will be the default value in 5.0
910

1011
4.0.0
1112
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Locale.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ class Locale extends Constraint
2929

3030
public $message = 'This value is not a valid locale.';
3131
public $canonicalize = false;
32+
33+
public function __construct($options = null)
34+
{
35+
if (!($options['canonicalize'] ?? false)) {
36+
@trigger_error('The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead..', E_USER_DEPRECATED);
37+
}
38+
39+
parent::__construct($options);
40+
}
3241
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/LocaleValidator.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public function validate($value, Constraint $constraint)
4040
throw new UnexpectedTypeException($value, 'string');
4141
}
4242

43-
$value = (string) $value;
43+
$inputValue = (string) $value;
44+
$value = $inputValue;
4445
if ($constraint->canonicalize) {
4546
$value = \Locale::canonicalize($value);
4647
}
@@ -49,7 +50,7 @@ public function validate($value, Constraint $constraint)
4950

5051
if (!isset($locales[$value]) && !in_array($value, $localeBundle->getAliases(), true)) {
5152
$this->context->buildViolation($constraint->message)
52-
->setParameter('{{ value }}', $this->formatValue($value))
53+
->setParameter('{{ value }}', $this->formatValue($inputValue))
5354
->setCode(Locale::NO_SUCH_LOCALE_ERROR)
5455
->addViolation();
5556
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/LocaleValidatorTest.php
+90-11Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,123 @@ protected function createValidator()
2222
return new LocaleValidator();
2323
}
2424

25-
public function testNullIsValid()
25+
/**
26+
* @group legacy
27+
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead..
28+
*
29+
* @dataProvider getValidLocales
30+
*/
31+
public function testLegacyNullIsValid()
2632
{
2733
$this->validator->validate(null, new Locale());
2834

2935
$this->assertNoViolation();
3036
}
3137

32-
public function testEmptyStringIsValid()
38+
public function testNullIsValid()
39+
{
40+
$this->validator->validate(null, new Locale(array('canonicalize' => true)));
41+
42+
$this->assertNoViolation();
43+
}
44+
45+
/**
46+
* @group legacy
47+
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead..
48+
*
49+
* @dataProvider getValidLocales
50+
*/
51+
public function testLegacyEmptyStringIsValid()
3352
{
3453
$this->validator->validate('', new Locale());
3554

3655
$this->assertNoViolation();
3756
}
3857

58+
public function testEmptyStringIsValid()
59+
{
60+
$this->validator->validate('', new Locale(array('canonicalize' => true)));
61+
62+
$this->assertNoViolation();
63+
}
64+
3965
/**
66+
* @group legacy
67+
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead..
4068
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
4169
*/
42-
public function testExpectsStringCompatibleType()
70+
public function testLegacyExpectsStringCompatibleType()
4371
{
4472
$this->validator->validate(new \stdClass(), new Locale());
4573
}
4674

4775
/**
76+
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
77+
*/
78+
public function testExpectsStringCompatibleType()
79+
{
80+
$this->validator->validate(new \stdClass(), new Locale(array('canonicalize' => true)));
81+
}
82+
83+
/**
84+
* @group legacy
85+
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead..
86+
*
4887
* @dataProvider getValidLocales
4988
*/
50-
public function testValidLocales($locale)
89+
public function testLegacyValidLocales(string $locale)
5190
{
5291
$this->validator->validate($locale, new Locale());
5392

5493
$this->assertNoViolation();
5594
}
5695

96+
/**
97+
* @dataProvider getValidLocales
98+
*/
99+
public function testValidLocales($locale, array $options)
100+
{
101+
$this->validator->validate($locale, new Locale($options));
102+
103+
$this->assertNoViolation();
104+
}
105+
57106
public function getValidLocales()
58107
{
59108
return array(
60-
array('en'),
61-
array('en_US'),
62-
array('pt'),
63-
array('pt_PT'),
64-
array('zh_Hans'),
65-
array('fil_PH'),
109+
array('en', array('canonicalize' => true)),
110+
array('en_US', array('canonicalize' => true)),
111+
array('pt', array('canonicalize' => true)),
112+
array('pt_PT', array('canonicalize' => true)),
113+
array('zh_Hans', array('canonicalize' => true)),
114+
array('fil_PH', array('canonicalize' => true)),
115+
);
116+
}
117+
118+
/**
119+
* @group legacy
120+
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead..
121+
* @dataProvider getLegacyInvalidLocales
122+
*/
123+
public function testLegacyInvalidLocales(string $locale)
124+
{
125+
$constraint = new Locale(array(
126+
'message' => 'myMessage',
127+
));
128+
129+
$this->validator->validate($locale, $constraint);
130+
131+
$this->buildViolation('myMessage')
132+
->setParameter('{{ value }}', '"'.$locale.'"')
133+
->setCode(Locale::NO_SUCH_LOCALE_ERROR)
134+
->assertRaised();
135+
}
136+
137+
public function getLegacyInvalidLocales()
138+
{
139+
return array(
140+
array('EN'),
141+
array('foobar'),
66142
);
67143
}
68144

@@ -73,6 +149,7 @@ public function testInvalidLocales($locale)
73149
{
74150
$constraint = new Locale(array(
75151
'message' => 'myMessage',
152+
'canonicalize' => true,
76153
));
77154

78155
$this->validator->validate($locale, $constraint);
@@ -86,12 +163,14 @@ public function testInvalidLocales($locale)
86163
public function getInvalidLocales()
87164
{
88165
return array(
89-
array('EN'),
166+
array('baz'),
90167
array('foobar'),
91168
);
92169
}
93170

94171
/**
172+
* @group legacy
173+
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead..
95174
* @dataProvider getUncanonicalizedLocales
96175
*/
97176
public function testInvalidLocalesWithoutCanonicalization(string $locale)

0 commit comments

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