Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41b9457

Browse files
lchruscielxabbuh
authored andcommitted
[Test] Reproduce issue with cascading validation
1 parent 7882c4a commit 41b9457

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorFunctionalTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Form\Exception\TransformationFailedException;
1818
use Symfony\Component\Form\Extension\Core\Type\DateType;
1919
use Symfony\Component\Form\Extension\Core\Type\FormType;
20+
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
2021
use Symfony\Component\Form\Extension\Core\Type\TextType;
2122
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
2223
use Symfony\Component\Form\FormBuilderInterface;
@@ -28,6 +29,7 @@
2829
use Symfony\Component\Validator\Constraints\GroupSequence;
2930
use Symfony\Component\Validator\Constraints\Length;
3031
use Symfony\Component\Validator\Constraints\NotBlank;
32+
use Symfony\Component\Validator\Constraints\Valid;
3133
use Symfony\Component\Validator\Mapping\ClassMetadata;
3234
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
3335
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
@@ -293,6 +295,39 @@ public function testCascadeValidationToChildFormsUsingPropertyPaths()
293295
$this->assertSame('children[field2].data', $violations[1]->getPropertyPath());
294296
}
295297

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+
296331
public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence()
297332
{
298333
$form = $this->formFactory->create(FormType::class, null, [
@@ -451,3 +486,62 @@ public function configureOptions(OptionsResolver $resolver)
451486
$resolver->setDefault('data_class', Foo::class);
452487
}
453488
}
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

Comments
 (0)