Skip to content

Commit 71295e7

Browse files
Merge branch '3.4' into 4.4
* 3.4: [PhpUnitBridge] fix syntax on PHP 5.3 [PhpUnitBridge] Fix undefined index when output of "composer show" cannot be parsed properly cascade validation to child forms [PhpUnitBridge] fix undefined var on version 3.4 bumped Symfony version to 3.4.42 updated VERSION for 3.4.41 update CONTRIBUTORS for 3.4.41 updated CHANGELOG for 3.4.41
2 parents ac13428 + ed064c2 commit 71295e7

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

Extension/Validator/Constraints/FormValidator.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public function validate($form, Constraint $formConstraint)
7272
if ($groups instanceof GroupSequence) {
7373
// Validate the data, the form AND nested fields in sequence
7474
$violationsCount = $this->context->getViolations()->count();
75-
$fieldPropertyPath = \is_object($data) ? 'children[%s]' : 'children%s';
7675

7776
foreach ($groups->groups as $group) {
7877
if ($validateDataGraph) {
@@ -91,7 +90,7 @@ public function validate($form, Constraint $formConstraint)
9190
// in different steps without breaking early enough
9291
$this->resolvedGroups[$field] = (array) $group;
9392
$fieldFormConstraint = new Form();
94-
$validator->atPath(sprintf($fieldPropertyPath, $field->getPropertyPath()))->validate($field, $fieldFormConstraint);
93+
$validator->atPath(sprintf('children[%s]', $field->getName()))->validate($field, $fieldFormConstraint);
9594
}
9695
}
9796

@@ -100,8 +99,6 @@ public function validate($form, Constraint $formConstraint)
10099
}
101100
}
102101
} else {
103-
$fieldPropertyPath = \is_object($data) ? 'children[%s]' : 'children%s';
104-
105102
if ($validateDataGraph) {
106103
$validator->atPath('data')->validate($data, null, $groups);
107104
}
@@ -138,7 +135,7 @@ public function validate($form, Constraint $formConstraint)
138135
if ($field->isSubmitted()) {
139136
$this->resolvedGroups[$field] = $groups;
140137
$fieldFormConstraint = new Form();
141-
$validator->atPath(sprintf($fieldPropertyPath, $field->getPropertyPath()))->validate($field, $fieldFormConstraint);
138+
$validator->atPath(sprintf('children[%s]', $field->getName()))->validate($field, $fieldFormConstraint);
142139
}
143140
}
144141
}

Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
1919
use Symfony\Component\Form\Extension\Validator\Constraints\Form;
2020
use Symfony\Component\Form\Extension\Validator\Constraints\FormValidator;
21+
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
2122
use Symfony\Component\Form\FormBuilder;
2223
use Symfony\Component\Form\FormFactoryBuilder;
2324
use Symfony\Component\Form\FormFactoryInterface;
@@ -52,7 +53,9 @@ class FormValidatorTest extends ConstraintValidatorTestCase
5253
protected function setUp(): void
5354
{
5455
$this->dispatcher = new EventDispatcher();
55-
$this->factory = (new FormFactoryBuilder())->getFormFactory();
56+
$this->factory = (new FormFactoryBuilder())
57+
->addExtension(new ValidatorExtension(Validation::createValidator()))
58+
->getFormFactory();
5659

5760
parent::setUp();
5861

@@ -834,6 +837,61 @@ public function testCompositeConstraintValidatedInSequence()
834837
$this->assertSame('data[field1]', $context->getViolations()[0]->getPropertyPath());
835838
}
836839

840+
public function testCascadeValidationToChildFormsUsingPropertyPaths()
841+
{
842+
$form = $this->getCompoundForm([], [
843+
'validation_groups' => ['group1', 'group2'],
844+
])
845+
->add('field1', null, [
846+
'constraints' => [new NotBlank(['groups' => 'group1'])],
847+
'property_path' => '[foo]',
848+
])
849+
->add('field2', null, [
850+
'constraints' => [new NotBlank(['groups' => 'group2'])],
851+
'property_path' => '[bar]',
852+
])
853+
;
854+
855+
$form->submit([
856+
'field1' => '',
857+
'field2' => '',
858+
]);
859+
860+
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
861+
$this->validator->initialize($context);
862+
$this->validator->validate($form, new Form());
863+
864+
$this->assertCount(2, $context->getViolations());
865+
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
866+
$this->assertSame('children[field1].data', $context->getViolations()[0]->getPropertyPath());
867+
$this->assertSame('This value should not be blank.', $context->getViolations()[1]->getMessage());
868+
$this->assertSame('children[field2].data', $context->getViolations()[1]->getPropertyPath());
869+
}
870+
871+
public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence()
872+
{
873+
$form = $this->getCompoundForm([], [
874+
'validation_groups' => new GroupSequence(['group1', 'group2']),
875+
])
876+
->add('field1', null, [
877+
'constraints' => [new NotBlank(['groups' => 'group1'])],
878+
'property_path' => '[foo]',
879+
])
880+
;
881+
882+
$form->submit([
883+
'field1' => '',
884+
]);
885+
886+
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
887+
$this->validator->initialize($context);
888+
$this->validator->validate($form, new Form());
889+
890+
$this->assertCount(1, $context->getViolations());
891+
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
892+
$this->assertSame('children[field1].data', $context->getViolations()[0]->getPropertyPath());
893+
}
894+
837895
protected function createValidator()
838896
{
839897
return new FormValidator();

0 commit comments

Comments
 (0)