8000 [Validator][Choice] Make strict the default option for choice validation by peterrehm · Pull Request #19257 · symfony/symfony · GitHub
[go: up one dir, main page]

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 1 commit
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
Prev Previous commit
Next Next commit
Deprecated setting the strict option to false in the choice constraint
  • Loading branch information
peterrehm committed Jul 12, 2016
commit fdb79728b601fc3e568d5351d41d449507668270
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/Constraints/Choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Choice extends Constraint
public $choices;
public $callback;
public $multiple = false;
public $strict = true;
public $strict = false;
public $min;
public $max;
public $message = 'The value you selected is not a valid choice.';
Expand Down
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 Down
0