8000 do not overwrite the constraint being evaluated · symfony/symfony@345a632 · GitHub
[go: up one dir, main page]

Skip to content

Commit 345a632

Browse files
committed
do not overwrite the constraint being evaluated
1 parent 5b1948e commit 345a632

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class FormValidator extends ConstraintValidator
2626
/**
2727
* {@inheritdoc}
2828
*/
29-
public function validate($form, Constraint $constraint)
29+
public function validate($form, Constraint $formConstraint)
3030
{
31-
if (!$constraint instanceof Form) {
32-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Form');
31+
if (!$formConstraint instanceof Form) {
32+
throw new UnexpectedTypeException($formConstraint, __NAMESPACE__.'\Form');
3333
}
3434

3535
if (!$form instanceof FormInterface) {
@@ -62,8 +62,8 @@ public function validate($form, Constraint $constraint)
6262
// Otherwise validate a constraint only once for the first
6363
// matching group
6464
foreach ($groups as $group) {
65-
if (\in_array($group, $constraint->groups)) {
66-
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
65+
if (\in_array($group, $formConstraint->groups)) {
66+
$validator->atPath('data')->validate($form->getData(), $formConstraint, $group);
6767
if (\count($this->context->getViolations()) > 0) {
6868
break;
6969
}
@@ -113,7 +113,7 @@ public function validate($form, Constraint $constraint)
113113
? (string) $form->getViewData()
114114
: \gettype($form->getViewData());
115115

116-
$this->context->setConstraint($constraint);
116+
$this->context->setConstraint($formConstraint);
117117
$this->context->buildViolation($config->getOption('invalid_message'))
118118
->setParameters(array_replace(['{{ value }}' => $clientDataAsString], $config->getOption('invalid_message_parameters')))
119119
->setInvalidValue($form->getViewData())
@@ -125,7 +125,7 @@ public function validate($form, Constraint $constraint)
125125

126126
// Mark the form with an error if it contains extra fields
127127
if (!$config->getOption('allow_extra_fields') && \count($form->getExtraData()) > 0) {
128-
$this->context->setConstraint($constraint);
128+
$this->context->setConstraint($formConstraint);
129129
$this->context->buildViolation($config->getOption('extra_fields_message'))
130130
->setParameter('{{ extra_fields }}', '"'.implode('", "', array_keys($form->getExtraData())).'"')
131131
->setInvalidValue($form->getExtraData())

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313

1414
use Symfony\Component\Form\CallbackTransformer;
1515
use Symfony\Component\Form\Exception\TransformationFailedException;
16+
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
1617
use Symfony\Component\Form\Extension\Validator\Constraints\Form;
1718
use Symfony\Component\Form\Extension\Validator\Constraints\FormValidator;
1819
use Symfony\Component\Form\FormBuilder;
1920
use Symfony\Component\Form\FormInterface;
2021
use Symfony\Component\Form\SubmitButtonBuilder;
22+
use Symfony\Component\Translation\IdentityTranslator;
2123
use Symfony\Component\Validator\Constraints\GroupSequence;
2224
use Symfony\Component\Validator\Constraints\NotBlank;
2325
use Symfony\Component\Validator\Constraints\NotNull;
2426
use Symfony\Component\Validator\Constraints\Valid;
27+
use Symfony\Component\Validator\Context\ExecutionContext;
2528
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
29+
use Symfony\Component\Validator\Validation;
2630

2731
/**
2832
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -649,6 +653,27 @@ public function getValidationGroups(FormInterface $form)
649653
return ['group1', 'group2'];
650654
}
651655

656+
public function testCauseForNotAllowedExtraFieldsIsTheFormConstraint()
657+
{
658+
$form = $this
659+
->getBuilder('form', null, ['constraints' => [new NotBlank(['groups' => ['foo']])]])
660+
->setCompound(true)
661+
->setDataMapper(new PropertyPathMapper())
662+
->getForm();
663+
$form->submit([
664+
'extra_data' => 'foo',
665+
]);
666+
667+
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
668+
$constraint = new Form();
669+
670+
$this->validator->initialize($context);
671+
$this->validator->validate($form, $constraint);
672+
673+
$this->assertCount(1, $context->getViolations());
674+
$this->assertSame($constraint, $context->getViolations()->get(0)->getConstraint());
675+
}
676+
652677
private function getMockExecutionContext()
653678
{
654679
$context = $this->getMockBuilder('Symfony\Component\Validator\Context\ExecutionContextInterface')->getMock();

0 commit comments

Comments
 (0)
0