Skip to content
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

[Form] do not overwrite the constraint being evaluated #30062

Merged
merged 1 commit into from Feb 7, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The $constraint name is also used below when the constraints configured through the constraints option are iterated, but we do not want to overwrite the reference to the constraint that is validated by this constraints.

throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Form');
if (!$formConstraint instanceof Form) {
throw new UnexpectedTypeException($formConstraint, __NAMESPACE__.'\Form');
}

if (!$form instanceof FormInterface) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down
Expand Up @@ -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 <bschussek@gmail.com>
Expand Down Expand Up @@ -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();
Expand Down