Skip to content

Commit cc145e2

Browse files
committed
bug #37345 [Form] collect all transformation failures (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [Form] collect all transformation failures | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #37262 | License | MIT | Doc PR | Commits ------- a9987ce collect all transformation failures
2 parents e707967 + a9987ce commit cc145e2

File tree

3 files changed

+64
-35
lines changed

3 files changed

+64
-35
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ public function validate($form, Constraint $formConstraint)
147147
foreach ($form as $child) {
148148
if (!$child->isSynchronized()) {
149149
$childrenSynchronized = false;
150-
break;
150+
151+
$fieldFormConstraint = new Form();
152+
$this->context->setNode($this->context->getValue(), $child, $this->context->getMetadata(), $this->context->getPropertyPath());
153+
$validator->atPath(sprintf('children[%s]', $child->getName()))->validate($child, $fieldFormConstraint);
151154
}
152155
}
153156

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Form\AbstractType;
16+
use Symfony\Component\Form\CallbackTransformer;
17+
use Symfony\Component\Form\Exception\TransformationFailedException;
18+
use Symfony\Component\Form\Extension\Core\Type\DateType;
1619
use Symfony\Component\Form\Extension\Core\Type\FormType;
1720
use Symfony\Component\Form\Extension\Core\Type\TextType;
1821
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
@@ -329,6 +332,63 @@ public function testContextIsPopulatedWithFormBeingValidatedUsingGroupSequence()
329332

330333
$this->assertCount(0, $violations);
331334
}
335+
336+
public function testSubmitFormChoiceInvalid()
337+
{
338+
$form = $this->formFactory->create(DateType::class, null, [
339+
'widget' => 'choice',
340+
'years' => [2021],
341+
]);
342+
343+
$form->submit([
344+
'year' => '2020',
345+
'month' => '13',
346+
'day' => '13',
347+
]);
348+
349+
$this->assertTrue($form->isSubmitted());
350+
$this->assertFalse($form->isValid());
351+
$this->assertCount(2, $form->getErrors());
352+
$this->assertSame('This value is not valid.', $form->getErrors()[0]->getMessage());
353+
$this->assertSame($form->get('year'), $form->getErrors()[0]->getOrigin());
354+
$this->assertSame('This value is not valid.', $form->getErrors()[1]->getMessage());
355+
$this->assertSame($form->get('month'), $form->getErrors()[1]->getOrigin());
356+
}
357+
358+
public function testDoNotAddInvalidMessageIfChildFormIsAlreadyNotSynchronized()
359+
{
360+
$formBuilder = $this->formFactory->createBuilder()
361+
->add('field1')
362+
->add('field2')
363+
->addModelTransformer(new CallbackTransformer(
364+
function () {
365+
},
366+
function () {
367+
throw new TransformationFailedException('This value is invalid.');
368+
}
369+
));
370+
$formBuilder->get('field2')->addModelTransformer(new CallbackTransformer(
371+
function () {
372+
},
373+
function () {
374+
throw new TransformationFailedException('This value is invalid.');
375+
}
376+
));
377+
$form = $formBuilder->getForm();
378+
379+
$form->submit([
380+
'field1' => 'foo',
381+
'field2' => 'bar',
382+
]);
383+
384+
$this->assertTrue($form->isSubmitted());
385+
$this->assertFalse($form->isValid());
386+
$this->assertCount(0, $form->getErrors());
387+
$this->assertTrue($form->get('field1')->isValid());
388+
$this->assertCount(0, $form->get('field1')->getErrors());
389+
$this->assertFalse($form->get('field2')->isValid());
390+
$this->assertCount(1, $form->get('field2')->getErrors());
391+
}
332392
}
333393

334394
class Foo

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

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -362,40 +362,6 @@ function () { throw new TransformationFailedException(); }
362362
->assertRaised();
363363
}
364364

365-
// https://github.com/symfony/symfony/issues/4359
366-
public function testDontMarkInvalidIfAnyChildIsNotSynchronized()
367-
{
368-
$object = new \stdClass();
369-
$object->child = 'bar';
370-
371-
$failingTransformer = new CallbackTransformer(
372-
function ($data) { return $data; },
373-
function () { throw new TransformationFailedException(); }
374-
);
375-
376-
$form = $this->getBuilder('name', '\stdClass')
377-
->setData($object)
378-
->addViewTransformer($failingTransformer)
379-
->setCompound(true)
380-
->setDataMapper(new PropertyPathMapper())
381-
->add(
382-
$this->getBuilder('child')
383-
->addViewTransformer($failingTransformer)
384-
)
385-
->getForm();
386-
387-
// Launch transformer
388-
$form->submit(['child' => 'foo']);
389-
390-
$this->assertTrue($form->isSubmitted());
391-
$this->assertFalse($form->isSynchronized());
392-
$this->expectNoValidate();
393-
394-
$this->validator->validate($form, new Form());
395-
396-
$this->assertNoViolation();
397-
}
398-
399365
public function testHandleGroupSequenceValidationGroups()
400366
{
401367
$object = new \stdClass();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy