From 345a6329dd735aed0b2942d5d354eb0ac32c29e5 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 2 Feb 2019 10:17:05 +0100 Subject: [PATCH] do not overwrite the constraint being evaluated --- .../Validator/Constraints/FormValidator.php | 14 +++++------ .../Constraints/FormValidatorTest.php | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php b/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php index 08d679a4b5d4c..b2c52ae1f329e 100644 --- a/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php +++ b/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php @@ -26,10 +26,10 @@ class FormValidator extends ConstraintValidator /** * {@inheritdoc} */ - public function validate($form, Constraint $constraint) + public function validate($form, Constraint $formConstraint) { - if (!$constraint instanceof Form) { - throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Form'); + if (!$formConstraint instanceof Form) { + throw new UnexpectedTypeException($formConstraint, __NAMESPACE__.'\Form'); } if (!$form instanceof FormInterface) { @@ -62,8 +62,8 @@ public function validate($form, Constraint $constraint) // Otherwise validate a constraint only once for the first // matching group foreach ($groups as $group) { - if (\in_array($group, $constraint->groups)) { - $validator->atPath('data')->validate($form->getData(), $constraint, $group); + if (\in_array($group, $formConstraint->groups)) { + $validator->atPath('data')->validate($form->getData(), $formConstraint, $group); if (\count($this->context->getViolations()) > 0) { break; } @@ -113,7 +113,7 @@ public function validate($form, Constraint $constraint) ? (string) $form->getViewData() : \gettype($form->getViewData()); - $this->context->setConstraint($constraint); + $this->context->setConstraint($formConstraint); $this->context->buildViolation($config->getOption('invalid_message')) ->setParameters(array_replace(['{{ value }}' => $clientDataAsString], $config->getOption('invalid_message_parameters'))) ->setInvalidValue($form->getViewData()) @@ -125,7 +125,7 @@ public function validate($form, Constraint $constraint) // Mark the form with an error if it contains extra fields if (!$config->getOption('allow_extra_fields') && \count($form->getExtraData()) > 0) { - $this->context->setConstraint($constraint); + $this->context->setConstraint($formConstraint); $this->context->buildViolation($config->getOption('extra_fields_message')) ->setParameter('{{ extra_fields }}', '"'.implode('", "', array_keys($form->getExtraData())).'"') ->setInvalidValue($form->getExtraData()) diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php index 399f2d70cafb5..89e491b63fbe9 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php @@ -13,16 +13,20 @@ use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Exception\TransformationFailedException; +use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; use Symfony\Component\Form\Extension\Validator\Constraints\Form; use Symfony\Component\Form\Extension\Validator\Constraints\FormValidator; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\SubmitButtonBuilder; +use Symfony\Component\Translation\IdentityTranslator; use Symfony\Component\Validator\Constraints\GroupSequence; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\Constraints\Valid; +use Symfony\Component\Validator\Context\ExecutionContext; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; +use Symfony\Component\Validator\Validation; /** * @author Bernhard Schussek @@ -649,6 +653,27 @@ public function getValidationGroups(FormInterface $form) return ['group1', 'group2']; } + public function testCauseForNotAllowedExtraFieldsIsTheFormConstraint() + { + $form = $this + ->getBuilder('form', null, ['constraints' => [new NotBlank(['groups' => ['foo']])]]) + ->setCompound(true) + ->setDataMapper(new PropertyPathMapper()) + ->getForm(); + $form->submit([ + 'extra_data' => 'foo', + ]); + + $context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator()); + $constraint = new Form(); + + $this->validator->initialize($context); + $this->validator->validate($form, $constraint); + + $this->assertCount(1, $context->getViolations()); + $this->assertSame($constraint, $context->getViolations()->get(0)->getConstraint()); + } + private function getMockExecutionContext() { $context = $this->getMockBuilder('Symfony\Component\Validator\Context\ExecutionContextInterface')->getMock(); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy