Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 016b3d5

Browse filesBrowse files
committed
collect all transformation failures
1 parent e707967 commit 016b3d5
Copy full SHA for 016b3d5

File tree

3 files changed

+69
-35
lines changed
Filter options

3 files changed

+69
-35
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Validator\Constraints\Valid;
1919
use Symfony\Component\Validator\ConstraintValidator;
2020
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
21+
use Symfony\Component\Validator\Validator\ContextualValidatorInterface;
2122

2223
/**
2324
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -146,8 +147,8 @@ public function validate($form, Constraint $formConstraint)
146147
/** @var FormInterface $child */
147148
foreach ($form as $child) {
148149
if (!$child->isSynchronized()) {
150+
$this->validateChildForm($validator, $child);
149151
$childrenSynchronized = false;
150-
break;
151152
}
152153
}
153154

@@ -250,4 +251,11 @@ private static function getConstraintsInGroups($constraints, $group)
250251
return \in_array($group, $constraint->groups, true);
251252
});
252253
}
254+
255+
private function validateChildForm(ContextualValidatorInterface $validator, FormInterface $form)
256+
{
257+
$fieldFormConstraint = new Form();
258+
$this->context->setNode($this->context->getValue(), $form, $this->context->getMetadata(), $this->context->getPropertyPath());
259+
$validator->atPath(sprintf('children[%s]', $form->getName()))->validate($form, $fieldFormConstraint);
260+
}
253261
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorFunctionalTest.php
+60Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php
-34Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.