8000 [Validator][Choice] Make strict the default option for choice validation · symfony/symfony@177c513 · GitHub
[go: up one dir, main page]

Skip to content

Commit 177c513

Browse files
peterrehmfabpot
authored andcommitted
[Validator][Choice] Make strict the default option for choice validation
1 parent 031b850 commit 177c513

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

UPGRADE-3.2.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,19 @@ Validator
7070
// ...
7171
}
7272
```
73+
74+
* Setting the strict option of the `Choice` Constraint to `false` has been
75+
deprecated and the option will be changed to `true` as of 4.0.
76+
77+
```php
78+
// ...
79+
use Symfony\Component\Validator\Constraints as Assert;
80+
81+
class MyEntity
82+
{
83+
/**
84+
* @Assert\Choice(choices={"MR", "MRS"}, strict=true)
85+
*/
86+
private $salutation;
87+
}
88+
```

UPGRADE-4.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,7 @@ Validator
261261
// ...
262262
}
263263
```
264+
265+
* The default value of the strict option of the `Choice` Constraint has been
266+
changed to `true` as of 4.0. If you need the the previous behaviour ensure to
267+
set the option to `false`.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function validate($value, Constraint $constraint)
5757
$choices = $constraint->choices;
5858
}
5959

60+
if (false === $constraint->strict) {
61+
@trigger_error('Setting the strict option of the Choice constraint to false is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED);
62+
}
63+
6064
if ($constraint->multiple) {
6165
foreach ($value as $_value) {
6266
if (!in_array($_value, $choices, $constraint->strict)) {

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

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,23 @@ public function testExpectArrayIfMultipleIsTrue()
4040
$constraint = new Choice(array(
4141
'choices' => array('foo', 'bar'),
4242
'multiple' => true,
43+
'strict' => true,
4344
));
4445

4546
$this->validator->validate('asdf', $constraint);
4647
}
4748

4849
public function testNullIsValid()
4950
{
50-
$this->validator->validate(null, new Choice(array('choices' => array('foo', 'bar'))));
51+
$this->validator->validate(
52+
null,
53+
new Choice(
54+
array(
55+
'choices' => array('foo', 'bar'),
56+
'strict' => true,
57+
)
58+
)
59+
);
5160

5261
$this->assertNoViolation();
5362
}
@@ -57,20 +66,20 @@ public function testNullIsValid()
5766
*/
5867
public function testChoicesOrCallbackExpected()
5968
{
60-
$this->validator->validate('foobar', new Choice());
69+
$this->validator->validate('foobar', new Choice(array('strict' => true)));
6170
}
6271

6372
/**
6473
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
6574
*/
6675
public function testValidCallbackExpected()
6776
{
68-
$this->validator->validate('foobar', new Choice(array('callback' => 'abcd')));
77+
$this->validator->validate('foobar', new Choice(array('callback' => 'abcd', 'strict' => true)));
6978
}
7079

7180
public function testValidChoiceArray()
7281
{
73-
$constraint = new Choice(array('choices' => array('foo', 'bar')));
82+
$constraint = new Choice(array('choices' => array('foo', 'bar'), 'strict' => true));
7483

7584
$this->validator->validate('bar', $constraint);
7685

@@ -79,7 +88,7 @@ public function testValidChoiceArray()
7988

8089
public function testValidChoiceCallbackFunction()
8190
{
82-
$constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback'));
91+
$constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback', 'strict' => true));
8392

8493
$this->validator->validate('bar', $constraint);
8594

@@ -88,9 +97,14 @@ public function testValidChoiceCallbackFunction()
8897

8998
public function testValidChoiceCallbackClosure()
9099
{
91-
$constraint = new Choice(array('callback' => function () {
92-
return array('foo', 'bar');
93-
}));
100+
$constraint = new Choice(
101+
array(
102+
'strict' => true,
103+
'callback' => function () {
104+
return array('foo', 'bar');
105+
},
106+
)
107+
);
94108

95109
$this->validator->validate('bar', $constraint);
96110

@@ -99,7 +113,7 @@ public function testValidChoiceCallbackClosure()
99113

100114
public function testValidChoiceCallbackStaticMethod()
101115
{
102-
$constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback')));
116+
$constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback'), 'strict' => true));
103117

104118
$this->validator->validate('bar', $constraint);
105119

@@ -111,7 +125,7 @@ public function testValidChoiceCallbackContextMethod()
111125
// search $this for "staticCallback"
112126
$this->setObject($this);
113127

114-
$constraint = new Choice(array('callback' => 'staticCallback'));
128+
$constraint = new Choice(array('callback' => 'staticCallback', 'strict' => true));
115129

116130
$this->validator->validate('bar', $constraint);
117131

@@ -123,6 +137,7 @@ public function testMultipleChoices()
123137
$constraint = new Choice(array(
124138
'choices' => array('foo', 'bar', 'baz'),
125139
'multiple' => true,
140+
'strict' => true,
126141
));
127142

128143
$this->validator->validate(array('baz', 'bar'), $constraint);
@@ -135,6 +150,7 @@ public function testInvalidChoice()
135150
$constraint = new Choice(array(
136151
'choices' => array('foo', 'bar'),
137152
'message' => 'myMessage',
153+
'strict' => true,
138154
));
139155

140156
$this->validator->validate('baz', $constraint);
@@ -152,6 +168,7 @@ public function testInvalidChoiceEmptyChoices()
152168
// the DB or the model
153169
'choices' => array(),
154170
'message' => 'myMessage',
171+
'strict' => true,
155172
));
156173

157174
$this->validator->validate('baz', $constraint);
@@ -168,6 +185,7 @@ public function testInvalidChoiceMultiple()
168185
'choices' => array('foo', 'bar'),
169186
'multipleMessage' => 'myMessage',
170187
'multiple' => true,
188+
'strict' => true,
171189
));
172190

173191
$this->validator->validate(array('foo', 'baz'), $constraint);
@@ -186,6 +204,7 @@ public function testTooFewChoices()
186204
'multiple' => true,
187205
'min' => 2,
188206
'minMessage' => 'myMessage',
207+
'strict' => true,
189208
));
190209

191210
$value = array('foo');
@@ -209,6 +228,7 @@ public function testTooManyChoices()
209228
'multiple' => true,
210229
'max' => 2,
211230
'maxMessage' => 'myMessage',
231+
'strict' => true,
212232
));
213233

214234
$value = array('foo', 'bar', 'moo');
@@ -225,6 +245,9 @@ public function testTooManyChoices()
225245
->assertRaised();
226246
}
227247

248+
/**
249+
* @group legacy
250+
*/
228251
public function testNonStrict()
229252
{
230253
$constraint = new Choice(array(
@@ -266,6 +289,9 @@ public function testStrictDisallowsDifferentType()
266289
->assertRaised();
267290
}
268291

292+
/**
293+
* @group legacy
294+
*/
269295
public function testNonStrictWithMultipleChoices()
270296
{
271297
$constraint = new Choice(array(

0 commit comments

Comments
 (0)
0