Skip to content

[Validator] Improve support for optional/required fields in Collection constraint #3118

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

Merged
merged 4 commits into from
Jan 16, 2012
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\Collection;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
class Optional extends Constraint
{
public $constraints = array();

public function getDefaultOption()
{
return 'constraints';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\Collection;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
class Required extends Constraint
{
public $constraints = array();

public function getDefaultOption()
{
return 'constraints';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Constraints\Collection\Optional;
use Symfony\Component\Validator\Constraints\Collection\Required;

/**
* @api
Expand Down Expand Up @@ -51,12 +53,18 @@ public function isValid($value, Constraint $constraint)
$extraFields[$field] = $fieldValue;
}

foreach ($constraint->fields as $field => $constraints) {
foreach ($constraint->fields as $field => $fieldConstraint) {
if (
// bug fix issue #2779
(is_array($value) && array_key_exists($field, $value)) ||
($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
// array instead of wrapped inside
$constraints = is_array($constraints) ? $constraints : array($constraints);
Expand All @@ -66,7 +74,7 @@ public function isValid($value, Constraint $constraint)
}

unset($extraFields[$field]);
} else {
} else if (!$fieldConstraint instanceof Optional) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be elseif without space

$missingFields[] = $field;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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\Tests\Component\Validator\Constraints;

class CollectionValidatorArrayObjectTest extends CollectionValidatorTest
{
public function prepareTestData(array $contents)
{
return new \ArrayObject($contents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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\Tests\Component\Validator\Constraints;

class CollectionValidatorArrayTest extends CollectionValidatorTest
{
public function prepareTestData(array $contents)
{
return $contents;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\Tests\Component\Validator\Constraints;

/**
* This class is a hand written simplified version of PHP native `ArrayObject`
* class, to show that it behaves different than PHP native implementation.
*/
class CustomArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
{
private $array;

public function __construct(array $array = null)
{
$this->array = (array) ($array ?: array());
}

public function offsetExists($offset)
{
return array_key_exists($offset, $this->array);
}

public function offsetGet($offset)
{
return $this->array[$offset];
}

public function offsetSet($offset, $value)
{
if (null === $offset) {
$this->array[] = $value;
} else {
$this->array[$offset] = $value;
}
}

public function offsetUnset($offset)
{
if (array_key_exists($offset, $this->array)) {
unset($this->array[$offset]);
}
}

public function getIterator()
{
return new \ArrayIterator($this->array);
}

public function count()
{
return count($this->array);
}

public function serialize()
{
return serialize($this->array);
}

public function unserialize($serialized)
{
$this->array = (array) unserialize((string) $serialized);
}
}

class CollectionValidatorCustomArrayObjectTest extends CollectionValidatorTest
{
public function prepareTestData(array $contents)
{
return new CustomArrayObject($contents);
}
}
Loading