Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions UPGRADE-3.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```
4 changes: 4 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function validate($value, Constraint $constraint)
$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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,23 @@ public function testExpectArrayIfMultipleIsTrue()
$constraint = new Choice(array(
'choices' => array('foo', 'bar'),
'multiple' => true,
'strict' => true,
));

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

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();
}
Expand All @@ -57,20 +66,20 @@ public function testNullIsValid()
*/
public function testChoicesOrCallbackExpected()
{
$this->validator->validate('foobar', new Choice());
$this->validator->validate('foobar', new Choice(array('strict' => true)));
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
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);

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

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);

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

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);

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

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);

Expand All @@ -111,7 +125,7 @@ public function testValidChoiceCallbackContextMethod()
// 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);

Expand All @@ -123,6 +137,7 @@ public function testMultipleChoices()
$constraint = new Choice(array(
'choices' => array('foo', 'bar', 'baz'),
'multiple' => true,
'strict' => true,
));

$this->validator->validate(array('baz', 'bar'), $constraint);
Expand All @@ -135,6 +150,7 @@ public function testInvalidChoice()
$constraint = new Choice(array(
'choices' => array('foo', 'bar'),
'message' => 'myMessage',
'strict' => true,
));

$this->validator->validate('baz', $constraint);
Expand All @@ -152,6 +168,7 @@ public function testInvalidChoiceEmptyChoices()
// the DB or the model
'choices' => array(),
'message' => 'myMessage',
'strict' => true,
));

$this->validator->validate('baz', $constraint);
Expand All @@ -168,6 +185,7 @@ public function testInvalidChoiceMultiple()
'choices' => array('foo', 'bar'),
'multipleMessage' => 'myMessage',
'multiple' => true,
'strict' => true,
));

$this->validator->validate(array('foo', 'baz'), $constraint);
Expand All @@ -186,6 +204,7 @@ public function testTooFewChoices()
'multiple' => true,
'min' => 2,
'minMessage' => 'myMessage',
'strict' => true,
));

$value = array('foo');
Expand All @@ -209,6 +228,7 @@ public function testTooManyChoices()
'multiple' => true,
'max' => 2,
'maxMessage' => 'myMessage',
'strict' => true,
));

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

/**
* @group legacy
*/
public function testNonStrict()
{
$constraint = new Choice(array(
Expand Down Expand Up @@ -266,6 +289,9 @@ public function testStrictDisallowsDifferentType()
->assertRaised();
}

/**
* @group legacy
*/
public function testNonStrictWithMultipleChoices()
{
$constraint = new Choice(array(
Expand Down