|
17 | 17 | use Symfony\Component\Form\Exception\TransformationFailedException;
|
18 | 18 | use Symfony\Component\Form\Extension\Core\Type\DateType;
|
19 | 19 | use Symfony\Component\Form\Extension\Core\Type\FormType;
|
| 20 | +use Symfony\Component\Form\Extension\Core\Type\IntegerType; |
20 | 21 | use Symfony\Component\Form\Extension\Core\Type\TextType;
|
21 | 22 | use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
|
22 | 23 | use Symfony\Component\Form\FormBuilderInterface;
|
|
28 | 29 | use Symfony\Component\Validator\Constraints\GroupSequence;
|
29 | 30 | use Symfony\Component\Validator\Constraints\Length;
|
30 | 31 | use Symfony\Component\Validator\Constraints\NotBlank;
|
| 32 | +use Symfony\Component\Validator\Constraints\Valid; |
31 | 33 | use Symfony\Component\Validator\Mapping\ClassMetadata;
|
32 | 34 | use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
|
33 | 35 | use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
|
@@ -293,6 +295,39 @@ public function testCascadeValidationToChildFormsUsingPropertyPaths()
|
293 | 295 | $this->assertSame('children[field2].data', $violations[1]->getPropertyPath());
|
294 | 296 | }
|
295 | 297 |
|
| 298 | + public function testCascadeValidationToChildFormsWithTwoValidConstraints() |
| 299 | + { |
| 300 | + $form = $this->formFactory->create(ReviewType::class); |
| 301 | + |
| 302 | + $form->submit([ |
| 303 | + 'rating' => 1, |
| 304 | + 'title' => 'Sample Title', |
| 305 | + ]); |
| 306 | + |
| 307 | + $violations = $this->validator->validate($form); |
| 308 | + |
| 309 | + $this->assertCount(1, $violations); |
| 310 | + $this->assertSame('This value should not be blank.', $violations[0]->getMessage()); |
| 311 | + $this->assertSame('children[author].data.email', $violations[0]->getPropertyPath()); |
| 312 | + } |
| 313 | + |
| 314 | + public function testCascadeValidationToChildFormsWithTwoValidConstraints2() |
| 315 | + { |
| 316 | + $form = $this->formFactory->create(ReviewType::class); |
| 317 | + |
| 318 | + $form->submit([ |
| 319 | + 'title' => 'Sample Title', |
| 320 | + ]); |
| 321 | + |
| 322 | + $violations = $this->validator->validate($form); |
| 323 | + |
| 324 | + $this->assertCount(2, $violations); |
| 325 | + $this->assertSame('This value should not be blank.', $violations[0]->getMessage()); |
| 326 | + $this->assertSame('data.rating', $violations[0]->getPropertyPath()); |
| 327 | + $this->assertSame('This value should not be blank.', $violations[1]->getMessage()); |
| 328 | + $this->assertSame('children[author].data.email', $violations[1]->getPropertyPath()); |
| 329 | + } |
| 330 | + |
296 | 331 | public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence()
|
297 | 332 | {
|
298 | 333 | $form = $this->formFactory->create(FormType::class, null, [
|
@@ -451,3 +486,62 @@ public function configureOptions(OptionsResolver $resolver)
|
451 | 486 | $resolver->setDefault('data_class', Foo::class);
|
452 | 487 | }
|
453 | 488 | }
|
| 489 | + |
| 490 | +class Review |
| 491 | +{ |
| 492 | + public $rating; |
| 493 | + public $title; |
| 494 | + public $author; |
| 495 | + |
| 496 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 497 | + { |
| 498 | + $metadata->addPropertyConstraint('title', new NotBlank()); |
| 499 | + $metadata->addPropertyConstraint('rating', new NotBlank()); |
| 500 | + } |
| 501 | +} |
| 502 | + |
| 503 | +class ReviewType extends AbstractType |
| 504 | +{ |
| 505 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 506 | + { |
| 507 | + $builder |
| 508 | + ->add('rating', IntegerType::class, [ |
| 509 | + 'constraints' => [new Valid()], |
| 510 | + ]) |
| 511 | + ->add('title') |
| 512 | + ->add('author', CustomerType::class, [ |
| 513 | + 'constraints' => [new Valid()], |
| 514 | + ]) |
| 515 | + ; |
| 516 | + } |
| 517 | + |
| 518 | + public function configureOptions(OptionsResolver $resolver) |
| 519 | + { |
| 520 | + $resolver->setDefault('data_class', Review::class); |
| 521 | + } |
| 522 | +} |
| 523 | + |
| 524 | +class Customer |
| 525 | +{ |
| 526 | + public $email; |
| 527 | + |
| 528 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 529 | + { |
| 530 | + $metadata->addPropertyConstraint('email', new NotBlank()); |
| 531 | + } |
| 532 | +} |
| 533 | + |
| 534 | +class CustomerType extends AbstractType |
| 535 | +{ |
| 536 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 537 | + { |
| 538 | + $builder |
| 539 | + ->add('email') |
| 540 | + ; |
| 541 | + } |
| 542 | + |
| 543 | + public function configureOptions(OptionsResolver $resolver) |
| 544 | + { |
| 545 | + $resolver->setDefault('data_class', Customer::class); |
| 546 | + } |
| 547 | +} |
0 commit comments