[Validator][Choice] Make strict the default option for choice validation
This commit is contained in:
parent
031b85029e
commit
177c513ece
@ -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;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
@ -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`.
|
||||||
|
@ -57,6 +57,10 @@ class ChoiceValidator extends ConstraintValidator
|
|||||||
$choices = $constraint->choices;
|
$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) {
|
if ($constraint->multiple) {
|
||||||
foreach ($value as $_value) {
|
foreach ($value as $_value) {
|
||||||
if (!in_array($_value, $choices, $constraint->strict)) {
|
if (!in_array($_value, $choices, $constraint->strict)) {
|
||||||
|
@ -40,6 +40,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
$constraint = new Choice(array(
|
$constraint = new Choice(array(
|
||||||
'choices' => array('foo', 'bar'),
|
'choices' => array('foo', 'bar'),
|
||||||
'multiple' => true,
|
'multiple' => true,
|
||||||
|
'strict' => true,
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->validator->validate('asdf', $constraint);
|
$this->validator->validate('asdf', $constraint);
|
||||||
@ -47,7 +48,15 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
|
|
||||||
public function testNullIsValid()
|
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();
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
@ -57,7 +66,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
*/
|
*/
|
||||||
public function testChoicesOrCallbackExpected()
|
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()
|
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()
|
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);
|
$this->validator->validate('bar', $constraint);
|
||||||
|
|
||||||
@ -79,7 +88,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
|
|
||||||
public function testValidChoiceCallbackFunction()
|
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);
|
$this->validator->validate('bar', $constraint);
|
||||||
|
|
||||||
@ -88,9 +97,14 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
|
|
||||||
public function testValidChoiceCallbackClosure()
|
public function testValidChoiceCallbackClosure()
|
||||||
{
|
{
|
||||||
$constraint = new Choice(array('callback' => function () {
|
$constraint = new Choice(
|
||||||
|
array(
|
||||||
|
'strict' => true,
|
||||||
|
'callback' => function () {
|
||||||
return array('foo', 'bar');
|
return array('foo', 'bar');
|
||||||
}));
|
},
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
$this->validator->validate('bar', $constraint);
|
$this->validator->validate('bar', $constraint);
|
||||||
|
|
||||||
@ -99,7 +113,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
|
|
||||||
public function testValidChoiceCallbackStaticMethod()
|
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);
|
$this->validator->validate('bar', $constraint);
|
||||||
|
|
||||||
@ -111,7 +125,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
// search $this for "staticCallback"
|
// search $this for "staticCallback"
|
||||||
$this->setObject($this);
|
$this->setObject($this);
|
||||||
|
|
||||||
$constraint = new Choice(array('callback' => 'staticCallback'));
|
$constraint = new Choice(array('callback' => 'staticCallback', 'strict' => true));
|
||||||
|
|
||||||
$this->validator->validate('bar', $constraint);
|
$this->validator->validate('bar', $constraint);
|
||||||
|
|
||||||
@ -123,6 +137,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
$constraint = new Choice(array(
|
$constraint = new Choice(array(
|
||||||
'choices' => array('foo', 'bar', 'baz'),
|
'choices' => array('foo', 'bar', 'baz'),
|
||||||
'multiple' => true,
|
'multiple' => true,
|
||||||
|
'strict' => true,
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->validator->validate(array('baz', 'bar'), $constraint);
|
$this->validator->validate(array('baz', 'bar'), $constraint);
|
||||||
@ -135,6 +150,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
$constraint = new Choice(array(
|
$constraint = new Choice(array(
|
||||||
'choices' => array('foo', 'bar'),
|
'choices' => array('foo', 'bar'),
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
|
'strict' => true,
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->validator->validate('baz', $constraint);
|
$this->validator->validate('baz', $constraint);
|
||||||
@ -152,6 +168,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
// the DB or the model
|
// the DB or the model
|
||||||
'choices' => array(),
|
'choices' => array(),
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
|
'strict' => true,
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->validator->validate('baz', $constraint);
|
$this->validator->validate('baz', $constraint);
|
||||||
@ -168,6 +185,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
'choices' => array('foo', 'bar'),
|
'choices' => array('foo', 'bar'),
|
||||||
'multipleMessage' => 'myMessage',
|
'multipleMessage' => 'myMessage',
|
||||||
'multiple' => true,
|
'multiple' => true,
|
||||||
|
'strict' => true,
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->validator->validate(array('foo', 'baz'), $constraint);
|
$this->validator->validate(array('foo', 'baz'), $constraint);
|
||||||
@ -186,6 +204,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
'multiple' => true,
|
'multiple' => true,
|
||||||
'min' => 2,
|
'min' => 2,
|
||||||
'minMessage' => 'myMessage',
|
'minMessage' => 'myMessage',
|
||||||
|
'strict' => true,
|
||||||
));
|
));
|
||||||
|
|
||||||
$value = array('foo');
|
$value = array('foo');
|
||||||
@ -209,6 +228,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
'multiple' => true,
|
'multiple' => true,
|
||||||
'max' => 2,
|
'max' => 2,
|
||||||
'maxMessage' => 'myMessage',
|
'maxMessage' => 'myMessage',
|
||||||
|
'strict' => true,
|
||||||
));
|
));
|
||||||
|
|
||||||
$value = array('foo', 'bar', 'moo');
|
$value = array('foo', 'bar', 'moo');
|
||||||
@ -225,6 +245,9 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
->assertRaised();
|
->assertRaised();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group legacy
|
||||||
|
*/
|
||||||
public function testNonStrict()
|
public function testNonStrict()
|
||||||
{
|
{
|
||||||
$constraint = new Choice(array(
|
$constraint = new Choice(array(
|
||||||
@ -266,6 +289,9 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase
|
|||||||
->assertRaised();
|
->assertRaised();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group legacy
|
||||||
|
*/
|
||||||
public function testNonStrictWithMultipleChoices()
|
public function testNonStrictWithMultipleChoices()
|
||||||
{
|
{
|
||||||
$constraint = new Choice(array(
|
$constraint = new Choice(array(
|
||||||
|
Reference in New Issue
Block a user