Skip to content

Commit fa3b8f8

Browse files
Merge branch '5.0' into 5.1
* 5.0: [Mime] Remove unused var [HttpClient] fix monitoring timeouts when other streams are active [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 Move ajax clear event listener initialization on loadToolbar [HttpClient] Throw JsonException instead of TransportException on empty response in Response::toArray() take into account the context when preserving empty array objects [VarExporter] tfix: s/markAsSkipped/markTestSkipped/ bumped Symfony version to 5.0.10 updated VERSION for 5.0.9 updated CHANGELOG for 5.0.9 bumped Symfony version to 4.4.10 updated VERSION for 4.4.9 updated CHANGELOG for 4.4.9 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 4d49269 + 6f1e0bb commit fa3b8f8

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

@@ -836,6 +839,61 @@ public function testCompositeConstraintValidatedInSequence()
836839
$this->assertSame('data[field1]', $context->getViolations()[0]->getPropertyPath());
837840
}
838841

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

0 commit comments

Comments
 (0)