[Validator][Choice] Make strict the default option for choice validation

This commit is contained in:
Peter Rehm 2016-07-01 09:59:41 +02:00 committed by Fabien Potencier
parent 031b85029e
commit 177c513ece
4 changed files with 60 additions and 10 deletions

View File

@ -70,3 +70,19 @@ Validator
// ...
}
```
* Setting the strict option of the `Choice` Constraint to `false` has been
deprecated and the option will be changed to `true` as of 4.0.
```php
// ...
use Symfony\Component\Validator\Constraints as Assert;
class MyEntity
{
/**
* @Assert\Choice(choices={"MR", "MRS"}, strict=true)
*/
private $salutation;
}
```

View File

@ -261,3 +261,7 @@ Validator
// ...
}
```
* The default value of the strict option of the `Choice` Constraint has been
changed to `true` as of 4.0. If you need the the previous behaviour ensure to
set the option to `false`.

View File

@ -57,6 +57,10 @@ class ChoiceValidator extends ConstraintValidator
$choices = $constraint->choices;
}
if (false === $constraint->strict) {
@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);
}
if ($constraint->multiple) {
foreach ($value as $_value) {
if (!in_array($_value, $choices, $constraint->strict)) {

View File

@ -40,6 +40,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
$constraint = new Choice(array(
'choices' => array('foo', 'bar'),
'multiple' => true,
'strict' => true,
));
$this->validator->validate('asdf', $constraint);
@ -47,7 +48,15 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
public function testNullIsValid()
{
$this->validator->validate(null, new Choice(array('choices' => array('foo', 'bar'))));
$this->validator->validate(
null,
new Choice(
array(
'choices' => array('foo', 'bar'),
'strict' => true,
)
)
);
$this->assertNoViolation();
}
@ -57,7 +66,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
*/
public function testChoicesOrCallbackExpected()
{
$this->validator->validate('foobar', new Choice());
$this->validator->validate('foobar', new Choice(array('strict' => true)));
}
/**
@ -65,12 +74,12 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
*/
public function testValidCallbackExpected()
{
$this->validator->validate('foobar', new Choice(array('callback' => 'abcd')));
$this->validator->validate('foobar', new Choice(array('callback' => 'abcd', 'strict' => true)));
}
public function testValidChoiceArray()
{
$constraint = new Choice(array('choices' => array('foo', 'bar')));
$constraint = new Choice(array('choices' => array('foo', 'bar'), 'strict' => true));
$this->validator->validate('bar', $constraint);
@ -79,7 +88,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
public function testValidChoiceCallbackFunction()
{
$constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback'));
$constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback', 'strict' => true));
$this->validator->validate('bar', $constraint);
@ -88,9 +97,14 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
public function testValidChoiceCallbackClosure()
{
$constraint = new Choice(array('callback' => function () {
return array('foo', 'bar');
}));
$constraint = new Choice(
array(
'strict' => true,
'callback' => function () {
return array('foo', 'bar');
},
)
);
$this->validator->validate('bar', $constraint);
@ -99,7 +113,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
public function testValidChoiceCallbackStaticMethod()
{
$constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback')));
$constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback'), 'strict' => true));
$this->validator->validate('bar', $constraint);
@ -111,7 +125,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
// search $this for "staticCallback"
$this->setObject($this);
$constraint = new Choice(array('callback' => 'staticCallback'));
$constraint = new Choice(array('callback' => 'staticCallback', 'strict' => true));
$this->validator->validate('bar', $constraint);
@ -123,6 +137,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
$constraint = new Choice(array(
'choices' => array('foo', 'bar', 'baz'),
'multiple' => true,
'strict' => true,
));
$this->validator->validate(array('baz', 'bar'), $constraint);
@ -135,6 +150,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
$constraint = new Choice(array(
'choices' => array('foo', 'bar'),
'message' => 'myMessage',
'strict' => true,
));
$this->validator->validate('baz', $constraint);
@ -152,6 +168,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
// the DB or the model
'choices' => array(),
'message' => 'myMessage',
'strict' => true,
));
$this->validator->validate('baz', $constraint);
@ -168,6 +185,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
'choices' => array('foo', 'bar'),
'multipleMessage' => 'myMessage',
'multiple' => true,
'strict' => true,
));
$this->validator->validate(array('foo', 'baz'), $constraint);
@ -186,6 +204,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
'multiple' => true,
'min' => 2,
'minMessage' => 'myMessage',
'strict' => true,
));
$value = array('foo');
@ -209,6 +228,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
'multiple' => true,
'max' => 2,
'maxMessage' => 'myMessage',
'strict' => true,
));
$value = array('foo', 'bar', 'moo');
@ -225,6 +245,9 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
->assertRaised();
}
/**
* @group legacy
*/
public function testNonStrict()
{
$constraint = new Choice(array(
@ -266,6 +289,9 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
->assertRaised();
}
/**
* @group legacy
*/
public function testNonStrictWithMultipleChoices()
{
$constraint = new Choice(array(