[Validator] Added constraints Optional and Required for the CollectionValidator
This commit is contained in:
parent
efada56312
commit
6641f3e231
|
@ -51,12 +51,18 @@ class CollectionValidator extends ConstraintValidator
|
||||||
$extraFields[$field] = $fieldValue;
|
$extraFields[$field] = $fieldValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($constraint->fields as $field => $constraints) {
|
foreach ($constraint->fields as $field => $fieldConstraint) {
|
||||||
if (
|
if (
|
||||||
// bug fix issue #2779
|
// bug fix issue #2779
|
||||||
(is_array($value) && array_key_exists($field, $value)) ||
|
(is_array($value) && array_key_exists($field, $value)) ||
|
||||||
($value instanceof \ArrayAccess && $value->offsetExists($field))
|
($value instanceof \ArrayAccess && $value->offsetExists($field))
|
||||||
) {
|
) {
|
||||||
|
if ($fieldConstraint instanceof Required || $fieldConstraint instanceof Optional) {
|
||||||
|
$constraints = $fieldConstraint->constraints;
|
||||||
|
} else {
|
||||||
|
$constraints = $fieldConstraint;
|
||||||
|
}
|
||||||
|
|
||||||
// cannot simply cast to array, because then the object is converted to an
|
// cannot simply cast to array, because then the object is converted to an
|
||||||
// array instead of wrapped inside
|
// array instead of wrapped inside
|
||||||
$constraints = is_array($constraints) ? $constraints : array($constraints);
|
$constraints = is_array($constraints) ? $constraints : array($constraints);
|
||||||
|
@ -66,7 +72,7 @@ class CollectionValidator extends ConstraintValidator
|
||||||
}
|
}
|
||||||
|
|
||||||
unset($extraFields[$field]);
|
unset($extraFields[$field]);
|
||||||
} else {
|
} else if (!$fieldConstraint instanceof Optional) {
|
||||||
$missingFields[] = $field;
|
$missingFields[] = $field;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
*
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
class Optional extends Constraint
|
||||||
|
{
|
||||||
|
public $constraints = array();
|
||||||
|
|
||||||
|
public function getDefaultOption()
|
||||||
|
{
|
||||||
|
return 'constraints';
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
*
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
class Required extends Constraint
|
||||||
|
{
|
||||||
|
public $constraints = array();
|
||||||
|
|
||||||
|
public function getDefaultOption()
|
||||||
|
{
|
||||||
|
return 'constraints';
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,6 +15,8 @@ use Symfony\Component\Validator\ExecutionContext;
|
||||||
use Symfony\Component\Validator\Constraints\Min;
|
use Symfony\Component\Validator\Constraints\Min;
|
||||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||||
use Symfony\Component\Validator\Constraints\NotNull;
|
use Symfony\Component\Validator\Constraints\NotNull;
|
||||||
|
use Symfony\Component\Validator\Constraints\Required;
|
||||||
|
use Symfony\Component\Validator\Constraints\Optional;
|
||||||
use Symfony\Component\Validator\Constraints\Collection;
|
use Symfony\Component\Validator\Constraints\Collection;
|
||||||
use Symfony\Component\Validator\Constraints\CollectionValidator;
|
use Symfony\Component\Validator\Constraints\CollectionValidator;
|
||||||
|
|
||||||
|
@ -309,6 +311,138 @@ class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testOptionalFieldPresent()
|
||||||
|
{
|
||||||
|
$array = array(
|
||||||
|
'foo' => null,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($this->validator->isValid($array, new Collection(array(
|
||||||
|
'foo' => new Optional(),
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOptionalFieldNotPresent()
|
||||||
|
{
|
||||||
|
$array = array(
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($this->validator->isValid($array, new Collection(array(
|
||||||
|
'foo' => new Optional(),
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOptionalFieldSingleConstraint()
|
||||||
|
{
|
||||||
|
$this->context->setGroup('MyGroup');
|
||||||
|
$this->context->setPropertyPath('bar');
|
||||||
|
|
||||||
|
$array = array(
|
||||||
|
'foo' => 5,
|
||||||
|
);
|
||||||
|
|
||||||
|
$constraint = new Min(4);
|
||||||
|
|
||||||
|
$this->walker->expects($this->once())
|
||||||
|
->method('walkConstraint')
|
||||||
|
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
|
||||||
|
|
||||||
|
$this->assertTrue($this->validator->isValid($array, new Collection(array(
|
||||||
|
'foo' => new Optional($constraint),
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOptionalFieldMultipleConstraints()
|
||||||
|
{
|
||||||
|
$this->context->setGroup('MyGroup');
|
||||||
|
$this->context->setPropertyPath('bar');
|
||||||
|
|
||||||
|
$array = array(
|
||||||
|
'foo' => 5,
|
||||||
|
);
|
||||||
|
|
||||||
|
$constraints = array(
|
||||||
|
new NotNull(),
|
||||||
|
new Min(4),
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($constraints as $i => $constraint) {
|
||||||
|
$this->walker->expects($this->at($i))
|
||||||
|
->method('walkConstraint')
|
||||||
|
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertTrue($this->validator->isValid($array, new Collection(array(
|
||||||
|
'foo' => new Optional($constraints),
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRequiredFieldPresent()
|
||||||
|
{
|
||||||
|
$array = array(
|
||||||
|
'foo' => null,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($this->validator->isValid($array, new Collection(array(
|
||||||
|
'foo' => new Required(),
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRequiredFieldNotPresent()
|
||||||
|
{
|
||||||
|
$array = array(
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertFalse($this->validator->isValid($array, new Collection(array(
|
||||||
|
'foo' => new Required(),
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRequiredFieldSingleConstraint()
|
||||||
|
{
|
||||||
|
$this->context->setGroup('MyGroup');
|
||||||
|
$this->context->setPropertyPath('bar');
|
||||||
|
|
||||||
|
$array = array(
|
||||||
|
'foo' => 5,
|
||||||
|
);
|
||||||
|
|
||||||
|
$constraint = new Min(4);
|
||||||
|
|
||||||
|
$this->walker->expects($this->once())
|
||||||
|
->method('walkConstraint')
|
||||||
|
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
|
||||||
|
|
||||||
|
$this->assertTrue($this->validator->isValid($array, new Collection(array(
|
||||||
|
'foo' => new Required($constraint),
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRequiredFieldMultipleConstraints()
|
||||||
|
{
|
||||||
|
$this->context->setGroup('MyGroup');
|
||||||
|
$this->context->setPropertyPath('bar');
|
||||||
|
|
||||||
|
$array = array(
|
||||||
|
'foo' => 5,
|
||||||
|
);
|
||||||
|
|
||||||
|
$constraints = array(
|
||||||
|
new NotNull(),
|
||||||
|
new Min(4),
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($constraints as $i => $constraint) {
|
||||||
|
$this->walker->expects($this->at($i))
|
||||||
|
->method('walkConstraint')
|
||||||
|
->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertTrue($this->validator->isValid($array, new Collection(array(
|
||||||
|
'foo' => new Required($constraints),
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
|
||||||
public function testObjectShouldBeLeftUnchanged()
|
public function testObjectShouldBeLeftUnchanged()
|
||||||
{
|
{
|
||||||
$value = new \ArrayObject(array(
|
$value = new \ArrayObject(array(
|
||||||
|
|
Reference in New Issue