|
18 | 18 | use Symfony\Component\Form\Exception\TransformationFailedException; |
19 | 19 | use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper; |
20 | 20 | use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper; |
| 21 | +use Symfony\Component\Form\FileUploadError; |
21 | 22 | use Symfony\Component\Form\Form; |
22 | 23 | use Symfony\Component\Form\FormConfigBuilder; |
23 | 24 | use Symfony\Component\Form\FormError; |
24 | 25 | use Symfony\Component\Form\FormInterface; |
25 | 26 | use Symfony\Component\Form\FormRenderer; |
26 | 27 | use Symfony\Component\Form\Tests\Extension\Validator\ViolationMapper\Fixtures\Issue; |
27 | 28 | use Symfony\Component\PropertyAccess\PropertyPath; |
| 29 | +use Symfony\Component\Validator\Constraints\File; |
28 | 30 | use Symfony\Component\Validator\ConstraintViolation; |
29 | 31 | use Symfony\Component\Validator\ConstraintViolationInterface; |
30 | 32 | use Symfony\Contracts\Translation\TranslatorInterface; |
@@ -83,6 +85,7 @@ protected function getForm($name = 'name', $propertyPath = null, $dataClass = nu |
83 | 85 | $config->setPropertyPath($propertyPath); |
84 | 86 | $config->setCompound(true); |
85 | 87 | $config->setDataMapper(new DataMapper()); |
| 88 | + $config->setErrorBubbling($options['error_bubbling'] ?? false); |
86 | 89 |
|
87 | 90 | if (!$synchronized) { |
88 | 91 | $config->addViewTransformer(new CallbackTransformer( |
@@ -1759,4 +1762,93 @@ public function testTranslatorNotCalledWithoutLabel() |
1759 | 1762 | $violation = new ConstraintViolation('Message without label', null, [], null, 'data.name', null); |
1760 | 1763 | $this->mapper->mapViolation($violation, $parent); |
1761 | 1764 | } |
| 1765 | + |
| 1766 | + public function testFileUploadErrorIsNotRemovedIfNoFileSizeConstraintViolationWasRaised() |
| 1767 | + { |
| 1768 | + $form = $this->getForm('form'); |
| 1769 | + $form->addError(new FileUploadError( |
| 1770 | + 'The file is too large. Allowed maximum size is 2 MB.', |
| 1771 | + 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.', |
| 1772 | + [ |
| 1773 | + '{{ limit }}' => '2', |
| 1774 | + '{{ suffix }}' => 'MB', |
| 1775 | + ] |
| 1776 | + )); |
| 1777 | + |
| 1778 | + $this->mapper->mapViolation($this->getConstraintViolation('data'), $form); |
| 1779 | + |
| 1780 | + $this->assertCount(2, $form->getErrors()); |
| 1781 | + } |
| 1782 | + |
| 1783 | + public function testFileUploadErrorIsRemovedIfFileSizeConstraintViolationWasRaised() |
| 1784 | + { |
| 1785 | + $form = $this->getForm('form'); |
| 1786 | + $form->addError(new FileUploadError( |
| 1787 | + 'The file is too large. Allowed maximum size is 2 MB.', |
| 1788 | + 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.', |
| 1789 | + [ |
| 1790 | + '{{ limit }}' => '2', |
| 1791 | + '{{ suffix }}' => 'MB', |
| 1792 | + ] |
| 1793 | + )); |
| 1794 | + |
| 1795 | + $violation = new ConstraintViolation( |
| 1796 | + 'The file is too large (3 MB). Allowed maximum size is 2 MB.', |
| 1797 | + 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.', |
| 1798 | + [ |
| 1799 | + '{{ limit }}' => '2', |
| 1800 | + '{{ size }}' => '3', |
| 1801 | + '{{ suffix }}' => 'MB', |
| 1802 | + ], |
| 1803 | + '', |
| 1804 | + 'data', |
| 1805 | + null, |
| 1806 | + null, |
| 1807 | + (string) \UPLOAD_ERR_INI_SIZE, |
| 1808 | + new File() |
| 1809 | + ); |
| 1810 | + $this->mapper->mapViolation($this->getConstraintViolation('data'), $form); |
| 1811 | + $this->mapper->mapViolation($violation, $form); |
| 1812 | + |
| 1813 | + $this->assertCount(2, $form->getErrors()); |
| 1814 | + } |
| 1815 | + |
| 1816 | + public function testFileUploadErrorIsRemovedIfFileSizeConstraintViolationWasRaisedOnFieldWithErrorBubbling() |
| 1817 | + { |
| 1818 | + $parent = $this->getForm('parent'); |
| 1819 | + $child = $this->getForm('child', 'file', null, [], false, true, [ |
| 1820 | + 'error_bubbling' => true, |
| 1821 | + ]); |
| 1822 | + $parent->add($child); |
| 1823 | + $child->addError(new FileUploadError( |
| 1824 | + 'The file is too large. Allowed maximum size is 2 MB.', |
| 1825 | + 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.', |
| 1826 | + [ |
| 1827 | + '{{ limit }}' => '2', |
| 1828 | + '{{ suffix }}' => 'MB', |
| 1829 | + ] |
| 1830 | + )); |
| 1831 | + |
| 1832 | + $violation = new ConstraintViolation( |
| 1833 | + 'The file is too large (3 MB). Allowed maximum size is 2 MB.', |
| 1834 | + 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.', |
| 1835 | + [ |
| 1836 | + '{{ limit }}' => '2', |
| 1837 | + '{{ size }}' => '3', |
| 1838 | + '{{ suffix }}' => 'MB', |
| 1839 | + ], |
| 1840 | + null, |
| 1841 | + 'data.file', |
| 1842 | + null, |
| 1843 | + null, |
| 1844 | + (string) \UPLOAD_ERR_INI_SIZE, |
| 1845 | + new File() |
| 1846 | + ); |
| 1847 | + $this->mapper->mapViolation($this->getConstraintViolation('data'), $parent); |
| 1848 | + $this->mapper->mapViolation($this->getConstraintViolation('data.file'), $parent); |
| 1849 | + $this->mapper->mapViolation($violation, $parent); |
| 1850 | + |
| 1851 | + $this->assertCount(3, $parent->getErrors()); |
| 1852 | + $this->assertCount(0, $child->getErrors()); |
| 1853 | + } |
1762 | 1854 | } |
0 commit comments