Skip to content

Commit d2a7994

Browse files
committed
feature #19257 [Validator][Choice] Make strict the default option for choice validation (peterrehm)
This PR was squashed before being merged into the 3.2-dev branch (closes #19257). Discussion ---------- [Validator][Choice] Make strict the default option for choice validation | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #18973 | License | MIT | Doc PR | - This is just the WIP as there are two options. 1. Just change default which would only possible to introduce in 4.x or in 3.2 if this BC break is considered as acceptable 2. Add a new option e.g. `strictComparison` which defaults to true in 4.x and deprecate the usage of the strict option for 3.2. 3. Just deprecate strict = false and remove the option but I would be against that as we remove flexibility which might be wanted. As per discussion I went ahead with option 3. We can then still decide if we want to remove the option entirely or eventually reenable setting strict to false in a later release. Commits ------- 177c513 [Validator][Choice] Make strict the default option for choice validation
2 parents d6d6a47 + 177c513 commit d2a7994

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
@@ -94,6 +94,22 @@ Validator
9494
}
9595
```
9696

97+
* Setting the strict option of the `Choice` Constraint to `false` has been
98+
deprecated and the option will be changed to `true` as of 4.0.
99+
100+
```php
101+
// ...
102+
use Symfony\Component\Validator\Constraints as Assert;
103+
104+
class MyEntity
105+
{
106+
/**
107+
* @Assert\Choice(choices={"MR", "MRS"}, strict=true)
108+
*/
109+
private $salutation;
110+
}
111+
```
112+
97113
Yaml
98114
----
99115

UPGRADE-4.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,7 @@ Validator
290290
// ...
291291
}
292292
```
293+
294+
* The default value of the strict option of the `Choice` Constraint has been
295+
changed to `true` as of 4.0. If you need the the previous behaviour ensure to
296+
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)