Skip to content

Commit c815e76

Browse files
Merge branch '3.4' into 4.4
* 3.4: collect all transformation failures
2 parents 00b2f4e + cc145e2 commit c815e76

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ public function validate($form, Constraint $formConstraint)
153153
foreach ($form as $child) {
154154
if (!$child->isSynchronized()) {
155155
$childrenSynchronized = false;
156-
break;
156+
157+
$fieldFormConstraint = new Form();
158+
$this->context->setNode($this->context->getValue(), $child, $this->context->getMetadata(), $this->context->getPropertyPath());
159+
$validator->atPath(sprintf('children[%s]', $child->getName()))->validate($child, $fieldFormConstraint);
157160
}
158161
}
159162

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

+60
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;
@@ -333,6 +336,63 @@ public function testContextIsPopulatedWithFormBeingValidatedUsingGroupSequence()
333336

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

338398
class Foo

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

-34
Original file line numberDiff line numberDiff line change
@@ -405,40 +405,6 @@ function () {
405405
;
406406
}
407407

408-
// https://github.com/symfony/symfony/issues/4359
409-
public function testDontMarkInvalidIfAnyChildIsNotSynchronized()
410-
{
411-
$object = new \stdClass();
412-
$object->child = 'bar';
413-
414-
$failingTransformer = new CallbackTransformer(
415-
function ($data) { return $data; },
416-
function () { throw new TransformationFailedException(); }
417-
);
418-
419-
$form = $this->getBuilder('name', '\stdClass')
420-
->setData($object)
421-
->addViewTransformer($failingTransformer)
422-
->setCompound(true)
423-
->setDataMapper(new PropertyPathMapper())
424-
->add(
425-
$this->getBuilder('child')
426-
->addViewTransformer($failingTransformer)
427-
)
428-
->getForm();
429-
430-
// Launch transformer
431-
$form->submit(['child' => 'foo']);
432-
433-
$this->assertTrue($form->isSubmitted());
434-
$this->assertFalse($form->isSynchronized());
435-
$this->expectNoValidate();
436-
437-
$this->validator->validate($form, new Form());
438-
439-
$this->assertNoViolation();
440-
}
441-
442408
public function testHandleGroupSequenceValidationGroups()
443409
{
444410
$object = new \stdClass();

0 commit comments

Comments
 (0)