|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Form\Tests\Extension\Core\Type;
|
13 | 13 |
|
| 14 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
| 15 | +use Symfony\Component\Form\Extension\Validator\ValidatorExtension; |
| 16 | +use Symfony\Component\Form\Forms; |
14 | 17 | use Symfony\Component\Form\Tests\Fixtures\MoneyType;
|
15 | 18 | use Symfony\Component\Form\Tests\Fixtures\MoneyTypeConverter;
|
16 | 19 | use Symfony\Component\PropertyAccess\PropertyPath;
|
|
23 | 26 | use Symfony\Component\Form\FormError;
|
24 | 27 | use Symfony\Component\Form\Tests\Fixtures\Money;
|
25 | 28 | use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
|
| 29 | +use Symfony\Component\Validator\Validation; |
26 | 30 |
|
27 | 31 | class FormTest_AuthorWithoutRefSetter
|
28 | 32 | {
|
@@ -729,6 +733,37 @@ public function testSimpleObjectMapperOption($initialData, $submittedData, $expe
|
729 | 733 | $this->assertEquals($expectedData, $form->getData());
|
730 | 734 | }
|
731 | 735 |
|
| 736 | + public function testSimpleObjectMapperOptionTransformationFailedExceptionIsConvertedAsFormError() |
| 737 | + { |
| 738 | + $money = new Money(20.5, 'EUR'); |
| 739 | + |
| 740 | + $factory = Forms::createFormFactoryBuilder() |
| 741 | + ->addExtensions(array(new ValidatorExtension(Validation::createValidator()))) |
| 742 | + ->getFormFactory() |
| 743 | + ; |
| 744 | + |
| 745 | + $form = $factory->create(MoneyType::class, $money); |
| 746 | + $form->submit(array('amount' => 'invalid_amount', 'currency' => 'USD')); |
| 747 | + |
| 748 | + $this->assertFalse($form->isValid()); |
| 749 | + $this->assertNull($form->getData()); |
| 750 | + $this->assertCount(1, $form->get('amount')->getErrors()); |
| 751 | + $error = $form->get('amount')->getErrors()[0]; |
| 752 | + $this->assertSame('This value is not valid.', $error->getMessage()); |
| 753 | + |
| 754 | + $form = $factory->create(MoneyType::class, $money, array( |
| 755 | + 'invalid_message' => "Money is not valid." |
| 756 | + ))->add('amount', TextType::class); // replace the NumberType by a simple TextType |
| 757 | + $form->submit(array('amount' => 'invalid_amount', 'currency' => 'USD')); |
| 758 | + |
| 759 | + $this->assertFalse($form->isValid()); |
| 760 | + $this->assertNull($form->getData()); |
| 761 | + $this->assertCount(1, $form->getErrors()); |
| 762 | + $this->assertSame('Money "amount" should be numeric.', $form->getTransformationFailure()->getMessage()); |
| 763 | + $error = $form->getErrors()[0]; |
| 764 | + $this->assertSame('Money is not valid.', $error->getMessage()); |
| 765 | + } |
| 766 | + |
732 | 767 | protected function getTestedType()
|
733 | 768 | {
|
734 | 769 | return 'Symfony\Component\Form\Extension\Core\Type\FormType';
|
|
0 commit comments