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