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