Skip to content

[2.2] [WIP][Validator][Constraints][Collection] walk constraints by groups #4453

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 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Mapping\ValueMetadata;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -37,17 +38,20 @@ public function validate($value, Constraint $constraint)
}

$group = $this->context->getGroup();
$map = new ValueMetadata();

foreach ($constraint->fields as $field => $fieldConstraint) {
$map->setConstraints($fieldConstraint->constraints);

if (
// bug fix issue #2779
(is_array($value) && array_key_exists($field, $value)) ||
($value instanceof \ArrayAccess && $value->offsetExists($field))
) {
foreach ($fieldConstraint->constraints as $constr) {
foreach ($map->findConstraints($group) as $constr) {
$this->context->validateValue($value[$field], $constr, '['.$field.']', $group);
}
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
} elseif ($map->hasConstraints($group) && !$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
$this->context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
'{{ field }}' => $field
), null);
Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Component/Validator/Mapping/ValueMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\Mapping;

class ValueMetadata extends ElementMetadata
{
/**
* Sets the constraint(s) for this value.
*
* @param array $constraints
*/
public function setConstraints(array $constraints)
{
$this->constraints = $constraints;
$constraintsByGroup = array();

array_walk($constraints, function($constraint) use (&$constraintsByGroup) {
foreach($constraint->groups as $group){
$constraintsByGroup[$group][] = $constraint;
}
});

$this->constraintsByGroup = $constraintsByGroup;
}

/**
* Returns whether this element has any constraints, optionally by group.
*
* @return Boolean
*/
public function hasConstraints($group = null)
{
return isset($group) ? !empty($this->constraintsByGroup[$group]) : count($this->constraints) > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testThrowsExceptionIfNotTraversable()

public function testWalkSingleConstraint()
{
$constraint = new Range(array('min' => 4));
$constraint = new Range(array('min' => 4, 'groups' => 'MyGroup'));

$array = array(
'foo' => 3,
Expand Down Expand Up @@ -106,8 +106,8 @@ public function testWalkSingleConstraint()
public function testWalkMultipleConstraints()
{
$constraints = array(
new Range(array('min' => 4)),
new NotNull(),
new Range(array('min' => 4, 'groups' => 'MyGroup')),
new NotNull(array('groups' => 'MyGroup')),
);

$array = array(
Expand Down Expand Up @@ -203,7 +203,7 @@ public function testMissingFieldsDisallowed()

$constraint = new Collection(array(
'fields' => array(
'foo' => new Range(array('min' => 4)),
'foo' => new Range(array('min' => 4, 'groups' => 'MyGroup')),
),
'missingFieldsMessage' => 'myMessage',
));
Expand Down Expand Up @@ -266,7 +266,7 @@ public function testOptionalFieldSingleConstraint()
'foo' => 5,
);

$constraint = new Range(array('min' => 4));
$constraint = new Range(array('min' => 4, 'groups' => 'MyGroup'));

$this->context->expects($this->once())
->method('validateValue')
Expand All @@ -289,8 +289,8 @@ public function testOptionalFieldMultipleConstraints()
);

$constraints = array(
new NotNull(),
new Range(array('min' => 4)),
new NotNull(array('groups' => 'MyGroup')),
new Range(array('min' => 4, 'groups' => 'MyGroup')),
);
$i = 1;

Expand Down Expand Up @@ -336,7 +336,9 @@ public function testRequiredFieldNotPresent()

$this->validator->validate($data, new Collection(array(
'fields' => array(
'foo' => new Required(),
'foo' => new Required(
new NotNull(array('groups' => 'MyGroup'))
),
),
'missingFieldsMessage' => 'myMessage',
)));
Expand All @@ -348,7 +350,7 @@ public function testRequiredFieldSingleConstraint()
'foo' => 5,
);

$constraint = new Range(array('min' => 4));
$constraint = new Range(array('min' => 4, 'groups' => 'MyGroup'));

$this->context->expects($this->once())
->method('validateValue')
Expand All @@ -371,8 +373,8 @@ public function testRequiredFieldMultipleConstraints()
);

$constraints = array(
new NotNull(),
new Range(array('min' => 4)),
new NotNull(array('groups' => 'MyGroup')),
new Range(array('min' => 4, 'groups' => 'MyGroup')),
);
$i = 1;

Expand Down Expand Up @@ -408,4 +410,58 @@ public function testObjectShouldBeLeftUnchanged()
'foo' => 3
), (array) $value);
}

public function testByGroup()
{
$array = array(
'foo' => 2,
);

$constraints = array(
new Range(array('min' => 5, 'groups' => 'MyGroup')),
new Range(array('min' => 3, 'groups' => 'YourGroup')),
);

$this->context->expects($this->once())
->method('validateValue')
->with($array['foo'], $constraints[0], '[foo]', 'MyGroup');

$this->context->expects($this->never())
->method('addViolationAt');

$data = $this->prepareTestData($array);

$this->validator->validate($data, new Collection(array(
'fields' => array('foo' => $constraints)
)));
}

public function testMultipleByGroup()
{
$array = array(
'bar' => 3,
'baz' => 5
);

$constraints = array(
new Optional($inner = new NotNull(array('groups' => 'MyGroup'))),
new Range(array('min' => 5, 'groups' => 'YourGroup')),
new Required($inner)
);

$this->context->expects($this->once())
->method('validateValue')
->with($array['baz'], $inner, '[baz]', 'MyGroup');

$this->context->expects($this->never())
->method('addViolationAt');

$data = $this->prepareTestData($array);

$this->validator->validate($data, new Collection(array(
'foo' => $constraints[0],
'bar' => $constraints[1],
'baz' => $constraints[2]
)));
}
}