|
21 | 21 | use Symfony\Component\Form\Test\ForwardCompatTestTrait;
|
22 | 22 | use Symfony\Component\OptionsResolver\OptionsResolver;
|
23 | 23 | use Symfony\Component\Validator\Constraints\Collection;
|
| 24 | +use Symfony\Component\Validator\Constraints\Expression; |
24 | 25 | use Symfony\Component\Validator\Constraints\GroupSequence;
|
25 | 26 | use Symfony\Component\Validator\Constraints\Length;
|
26 | 27 | use Symfony\Component\Validator\Constraints\NotBlank;
|
@@ -283,6 +284,51 @@ public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSe
|
283 | 284 | $this->assertSame('This value should not be blank.', $violations[0]->getMessage());
|
284 | 285 | $this->assertSame('children[field1].data', $violations[0]->getPropertyPath());
|
285 | 286 | }
|
| 287 | + |
| 288 | + public function testContextIsPopulatedWithFormBeingValidated() |
| 289 | + { |
| 290 | + $form = $this->formFactory->create(FormType::class) |
| 291 | + ->add('field1', null, [ |
| 292 | + 'constraints' => [new Expression([ |
| 293 | + 'expression' => '!this.getParent().get("field2").getData()', |
| 294 | + ])], |
| 295 | + ]) |
| 296 | + ->add('field2') |
| 297 | + ; |
| 298 | + |
| 299 | + $form->submit([ |
| 300 | + 'field1' => '', |
| 301 | + 'field2' => '', |
| 302 | + ]); |
| 303 | + |
| 304 | + $violations = $this->validator->validate($form); |
| 305 | + |
| 306 | + $this->assertCount(0, $violations); |
| 307 | + } |
| 308 | + |
| 309 | + public function testContextIsPopulatedWithFormBeingValidatedUsingGroupSequence() |
| 310 | + { |
| 311 | + $form = $this->formFactory->create(FormType::class, null, [ |
| 312 | + 'validation_groups' => new GroupSequence(['group1']), |
| 313 | + ]) |
| 314 | + ->add('field1', null, [ |
| 315 | + 'constraints' => [new Expression([ |
| 316 | + 'expression' => '!this.getParent().get("field2").getData()', |
| 317 | + 'groups' => ['group1'], |
| 318 | + ])], |
| 319 | + ]) |
| 320 | + ->add('field2') |
| 321 | + ; |
| 322 | + |
| 323 | + $form->submit([ |
| 324 | + 'field1' => '', |
| 325 | + 'field2' => '', |
| 326 | + ]); |
| 327 | + |
| 328 | + $violations = $this->validator->validate($form); |
| 329 | + |
| 330 | + $this->assertCount(0, $violations); |
| 331 | + } |
286 | 332 | }
|
287 | 333 |
|
288 | 334 | class Foo
|
|
0 commit comments