Skip to content

Commit

Permalink
[Validator][Choice] Make strict the default option for choice validation
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrehm authored and fabpot committed Sep 14, 2016
1 parent 189d724 commit a76925e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Constraints/ChoiceValidator.php
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
46 changes: 36 additions & 10 deletions Tests/Constraints/ChoiceValidatorTest.php
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

0 comments on commit a76925e

Please sign in to comment.