bug #37767 [Form] fix mapping errors from unmapped forms (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] fix mapping errors from unmapped forms

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #10519
| License       | MIT
| Doc PR        |

Commits
-------

235920a388 fix mapping errors from unmapped forms
This commit is contained in:
Fabien Potencier 2020-08-11 11:39:48 +02:00
commit 0e9cd90857
3 changed files with 49 additions and 12 deletions

View File

@ -241,13 +241,6 @@ class ViolationMapper implements ViolationMapperInterface
// Form inherits its parent data
// Cut the piece out of the property path and proceed
$propertyPathBuilder->remove($i);
} elseif (!$scope->getConfig()->getMapped()) {
// Form is not mapped
// Set the form as new origin and strip everything
// we have so far in the path
$origin = $scope;
$propertyPathBuilder->remove(0, $i + 1);
$i = 0;
} else {
/* @var \Symfony\Component\PropertyAccess\PropertyPathInterface $propertyPath */
$propertyPath = $scope->getPropertyPath();

View File

@ -0,0 +1,16 @@
<?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\Form\Tests\Extension\Validator\ViolationMapper\Fixtures;
class Issue
{
}

View File

@ -22,6 +22,7 @@ use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigBuilder;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Tests\Extension\Validator\ViolationMapper\Fixtures\Issue;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationInterface;
@ -70,12 +71,12 @@ class ViolationMapperTest extends TestCase
$this->params = ['foo' => 'bar'];
}
protected function getForm($name = 'name', $propertyPath = null, $dataClass = null, $errorMapping = [], $inheritData = false, $synchronized = true)
protected function getForm($name = 'name', $propertyPath = null, $dataClass = null, $errorMapping = [], $inheritData = false, $synchronized = true, array $options = [])
{
$config = new FormConfigBuilder($name, $dataClass, $this->dispatcher, [
'error_mapping' => $errorMapping,
]);
$config->setMapped(true);
] + $options);
$config->setMapped(isset($options['mapped']) ? $options['mapped'] : true);
$config->setInheritData($inheritData);
$config->setPropertyPath($propertyPath);
$config->setCompound(true);
@ -96,9 +97,9 @@ class ViolationMapperTest extends TestCase
*
* @return ConstraintViolation
*/
protected function getConstraintViolation($propertyPath)
protected function getConstraintViolation($propertyPath, $root = null)
{
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, $propertyPath, null);
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, $root, $propertyPath, null);
}
/**
@ -112,6 +113,33 @@ class ViolationMapperTest extends TestCase
return $error;
}
public function testMappingErrorsWhenFormIsNotMapped()
{
$form = $this->getForm('name', null, Issue::class, [
'child1' => 'child2',
]);
$violation = $this->getConstraintViolation('children[child1].data', $form);
$child1Form = $this->getForm('child1', null, null, [], false, true, [
'mapped' => false,
]);
$child2Form = $this->getForm('child2', null, null, [], false, true, [
'mapped' => false,
]);
$form->add($child1Form);
$form->add($child2Form);
$form->submit([]);
$this->mapper->mapViolation($violation, $form);
$this->assertCount(0, $form->getErrors());
$this->assertCount(0, $form->get('child1')->getErrors());
$this->assertCount(1, $form->get('child2')->getErrors());
}
public function testMapToFormInheritingParentDataIfDataDoesNotMatch()
{
$violation = $this->getConstraintViolation('children[address].data.foo');