10000 constraint_validation option can now be an array by odolbeau · Pull Request #4329 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

constraint_validation option can now be an array #4329

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 1 commit 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 @@ -22,6 +22,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Validator\ExecutionContext;
use Symfony\Component\Validator\ConstraintViolationList;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -141,11 +142,25 @@ public function validateForm(DataEvent $event)
// Validation of the data in the custom group is done by validateData(),
// which is constrained by the Execute constraint
if ($form->hasAttribute('validation_constraint')) {
$violations = $this->validator->validateValue(
$form->getData(),
$form->getAttribute('validation_constraint'),
self::getFormValidationGroups($form)
);
if (is_array($form->getAttribute('validation_constraint'))) {
$violations = new ConstraintViolationList();
foreach ($form->getAttribute('validation_constraint') as $constraint) {
$newViolations = $this->validator->validateValue(
$form->getData(),
$constraint,
self::getFormValidationGroups($form)
);
if (null !== $newViolations) {
$violations->addAll($newViolations);
}
}
} else {
$violations = $this->validator->validateValue(
$form->getData(),
$form->getAttribute('validation_constraint'),
self::getFormValidationGroups($form)
);
}

if ($violations) {
foreach ($violations as $violation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function getValidationGroups(FormInterface $form)
return array('group1', 'group2');
}

public function testUseValidateValueWhenValidationConstraintExist()
public function testUseValidateValueWhenValidationConstraintIsAValidConstraint()
{
$constraint = $this->getMockForAbstractClass('Symfony\Component\Validator\Constraint');
$form = $this
Expand All @@ -131,6 +131,20 @@ public function testUseValidateValueWhenValidationConstraintExist()
$this->listener->validateForm(new DataEvent($form, null));
}

public function testUseValidateValueWhenValidationConstraintIsAnArray()
{
$constraint1 = $this->getMockForAbstractClass('Symfony\Component\Validator\Constraint');
$constraint2 = $this->getMockForAbstractClass('Symfony\Component\Validator\Constraint');
$form = $this
->getBuilder('name')
->setAttribute('validation_constraint', array($constraint1, $constraint2))
->getForm();

$this->delegate->expects($this->exactly(2))->method('validateValue');

$this->listener->validateForm(new DataEvent($form, null));
}

public function testFormErrorsOnForm()
{
$form = $this->getForm();
Expand Down
0