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

Skip to content

Commit 21ef411

Browse files
committed
bug #39333 [Form] do not apply the Valid constraint on scalar form data (lchrusciel, xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [Form] do not apply the Valid constraint on scalar form data | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38039 | License | MIT | Doc PR | #SymfonyHackday Commits ------- 453bb3e do not apply the Valid constraint on scalar form data 41b9457 [Test] Reproduce issue with cascading validation
2 parents 9354958 + 453bb3e commit 21ef411

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ public function validate($form, Constraint $formConstraint)
110110
foreach ($constraints as $constraint) {
111111
// For the "Valid" constraint, validate the data in all groups
112112
if ($constraint instanceof Valid) {
113-
$validator->atPath('data')->validate($data, $constraint, $groups);
113+
if (\is_object($data)) {
114+
$validator->atPath('data')->validate($data, $constraint, $groups);
115+
}
114116

115117
continue;
116118
}

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)