|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | 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; |
16 | 19 | use Symfony\Component\Form\Extension\Core\Type\FormType;
|
17 | 20 | use Symfony\Component\Form\Extension\Core\Type\TextType;
|
18 | 21 | use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
|
@@ -333,6 +336,63 @@ public function testContextIsPopulatedWithFormBeingValidatedUsingGroupSequence()
|
333 | 336 |
|
334 | 337 | $this->assertCount(0, $violations);
|
335 | 338 | }
|
| 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 | + } |
336 | 396 | }
|
337 | 397 |
|
338 | 398 | class Foo
|
|
0 commit comments