|
18 | 18 | use Symfony\Component\Form\Exception\TransformationFailedException;
|
19 | 19 | use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
|
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\Tests\Extension\Validator\ViolationMapper\Fixtures\Issue;
|
26 | 27 | use Symfony\Component\PropertyAccess\PropertyPath;
|
| 28 | +use Symfony\Component\Validator\Constraints\File; |
27 | 29 | use Symfony\Component\Validator\ConstraintViolation;
|
28 | 30 | use Symfony\Component\Validator\ConstraintViolationInterface;
|
29 | 31 |
|
@@ -81,6 +83,7 @@ protected function getForm($name = 'name', $propertyPath = null, $dataClass = nu
|
81 | 83 | $config->setPropertyPath($propertyPath);
|
82 | 84 | $config->setCompound(true);
|
83 | 85 | $config->setDataMapper(new PropertyPathMapper());
|
| 86 | + $config->setErrorBubbling($options['error_bubbling'] ?? false); |
84 | 87 |
|
85 | 88 | if (!$synchronized) {
|
86 | 89 | $config->addViewTransformer(new CallbackTransformer(
|
@@ -1590,4 +1593,93 @@ public function testBacktrackIfSeveralSubFormsWithSamePropertyPath()
|
1590 | 1593 | $this->assertEquals([$this->getFormError($violation2, $grandChild2)], iterator_to_array($grandChild2->getErrors()), $grandChild2->getName().' should have an error, but has none');
|
1591 | 1594 | $this->assertEquals([$this->getFormError($violation3, $grandChild3)], iterator_to_array($grandChild3->getErrors()), $grandChild3->getName().' should have an error, but has none');
|
1592 | 1595 | }
|
| 1596 | + |
| 1597 | + public function testFileUploadErrorIsNotRemovedIfNoFileSizeConstraintViolationWasRaised() |
| 1598 | + { |
| 1599 | + $form = $this->getForm('form'); |
| 1600 | + $form->addError(new FileUploadError( |
| 1601 | + 'The file is too large. Allowed maximum size is 2 MB.', |
| 1602 | + 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.', |
| 1603 | + [ |
| 1604 | + '{{ limit }}' => '2', |
| 1605 | + '{{ suffix }}' => 'MB', |
| 1606 | + ] |
| 1607 | + )); |
| 1608 | + |
| 1609 | + $this->mapper->mapViolation($this->getConstraintViolation('data'), $form); |
| 1610 | + |
| 1611 | + $this->assertCount(2, $form->getErrors()); |
| 1612 | + } |
| 1613 | + |
| 1614 | + public function testFileUploadErrorIsRemovedIfFileSizeConstraintViolationWasRaised() |
| 1615 | + { |
| 1616 | + $form = $this->getForm('form'); |
| 1617 | + $form->addError(new FileUploadError( |
| 1618 | + 'The file is too large. Allowed maximum size is 2 MB.', |
| 1619 | + 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.', |
| 1620 | + [ |
| 1621 | + '{{ limit }}' => '2', |
| 1622 | + '{{ suffix }}' => 'MB', |
| 1623 | + ] |
| 1624 | + )); |
| 1625 | + |
| 1626 | + $violation = new ConstraintViolation( |
| 1627 | + 'The file is too large (3 MB). Allowed maximum size is 2 MB.', |
| 1628 | + 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.', |
| 1629 | + [ |
| 1630 | + '{{ limit }}' => '2', |
| 1631 | + '{{ size }}' => '3', |
| 1632 | + '{{ suffix }}' => 'MB', |
| 1633 | + ], |
| 1634 | + '', |
| 1635 | + 'data', |
| 1636 | + null, |
| 1637 | + null, |
| 1638 | + (string) \UPLOAD_ERR_INI_SIZE, |
| 1639 | + new File() |
| 1640 | + ); |
| 1641 | + $this->mapper->mapViolation($this->getConstraintViolation('data'), $form); |
| 1642 | + $this->mapper->mapViolation($violation, $form); |
| 1643 | + |
| 1644 | + $this->assertCount(2, $form->getErrors()); |
| 1645 | + } |
| 1646 | + |
| 1647 | + public function testFileUploadErrorIsRemovedIfFileSizeConstraintViolationWasRaisedOnFieldWithErrorBubbling() |
| 1648 | + { |
| 1649 | + $parent = $this->getForm('parent'); |
| 1650 | + $child = $this->getForm('child', 'file', null, [], false, true, [ |
| 1651 | + 'error_bubbling' => true, |
| 1652 | + ]); |
| 1653 | + $parent->add($child); |
| 1654 | + $child->addError(new FileUploadError( |
| 1655 | + 'The file is too large. Allowed maximum size is 2 MB.', |
| 1656 | + 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.', |
| 1657 | + [ |
| 1658 | + '{{ limit }}' => '2', |
| 1659 | + '{{ suffix }}' => 'MB', |
| 1660 | + ] |
| 1661 | + )); |
| 1662 | + |
| 1663 | + $violation = new ConstraintViolation( |
| 1664 | + 'The file is too large (3 MB). Allowed maximum size is 2 MB.', |
| 1665 | + 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.', |
| 1666 | + [ |
| 1667 | + '{{ limit }}' => '2', |
| 1668 | + '{{ size }}' => '3', |
| 1669 | + '{{ suffix }}' => 'MB', |
| 1670 | + ], |
| 1671 | + null, |
| 1672 | + 'data.file', |
| 1673 | + null, |
| 1674 | + null, |
| 1675 | + (string) \UPLOAD_ERR_INI_SIZE, |
| 1676 | + new File() |
| 1677 | + ); |
| 1678 | + $this->mapper->mapViolation($this->getConstraintViolation('data'), $parent); |
| 1679 | + $this->mapper->mapViolation($this->getConstraintViolation('data.file'), $parent); |
| 1680 | + $this->mapper->mapViolation($violation, $parent); |
| 1681 | + |
| 1682 | + $this->assertCount(3, $parent->getErrors()); |
| 1683 | + $this->assertCount(0, $child->getErrors()); |
| 1684 | + } |
1593 | 1685 | }
|
0 commit comments