8000 prevent duplicated error message for file upload limits · symfony/symfony@fe6a2dd · GitHub
[go: up one dir, main page]

Skip to content

Commit fe6a2dd

Browse files
committed
prevent duplicated error message for file upload limits
1 parent b60bb6e commit fe6a2dd

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed

src/Symfony/Component/Form/Extension/Core/Type/FileType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\FileUploadError;
1516
use Symfony\Component\Form\FormBuilderInterface;
16-
use Symfony\Component\Form\FormError;
1717
use Symfony\Component\Form\FormEvent;
1818
use Symfony\Component\Form\FormEvents;
1919
use Symfony\Component\Form\FormInterface;
@@ -171,7 +171,7 @@ private function getFileUploadError(int $errorCode)
171171
$message = strtr($messageTemplate, $messageParameters);
172172
}
173173

174-
return new FormError($message, $messageTemplate, $messageParameters);
174+
return new FileUploadError($message, $messageTemplate, $messageParameters);
175175
}
176176

177177
/**

src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
namespace Symfony\Component\Form\Extension\Validator\ViolationMapper;
1313

14+
use Symfony\Component\Form\FileUploadError;
1415
use Symfony\Component\Form\FormError;
1516
use Symfony\Component\Form\FormInterface;
1617
use Symfony\Component\Form\Util\InheritDataAwareIterator;
1718
use Symfony\Component\PropertyAccess\PropertyPathBuilder;
1819
use Symfony\Component\PropertyAccess\PropertyPathIterator;
1920
use Symfony\Component\PropertyAccess\PropertyPathIteratorInterface;
21+
use Symfony\Component\Validator\Constraints\File;
2022
use Symfony\Component\Validator\ConstraintViolation;
2123

2224
/**
@@ -124,6 +126,23 @@ public function mapViolation(ConstraintViolation $violation, FormInterface $form
124126

125127
// Only add the error if the form is synchronized
126128
if ($this->acceptsErrors($scope)) {
129+
if ($violation->getConstraint() instanceof File && (string) \UPLOAD_ERR_INI_SIZE === $violation->getCode()) {
130+
$errorsTarget = $scope;
131+
132+
while (null !== $errorsTarget->getParent() && $errorsTarget->getConfig()->getErrorBubbling()) {
133+
$errorsTarget = $errorsTarget->getParent();
134+
}
135+
136+
$errors = $errorsTarget->getErrors();
137+
$errorsTarget->clearErrors();
138+
139+
foreach ($errors as $error) {
140+
if (!$error instanceof FileUploadError) {
141+
$errorsTarget->addError($error);
142+
}
143+
}
144+
}
145+
127146
$scope->addError(new FormError(
128147
$violation->getMessage(),
129148
$violation->getMessageTemplate(),
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form;
13+
14+
/**
15+
* @internal
16+
*/
17+
class FileUploadError extends FormError
18+
{
19+
}

src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
use Symfony\Component\Form\Exception\TransformationFailedException;
1919
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
2020
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;
21+
use Symfony\Component\Form\FileUploadError;
2122
use Symfony\Component\Form\Form;
2223
use Symfony\Component\Form\FormConfigBuilder;
2324
use Symfony\Component\Form\FormError;
2425
use Symfony\Component\Form\FormInterface;
2526
use Symfony\Component\Form\Tests\Extension\Validator\ViolationMapper\Fixtures\Issue;
2627
use Symfony\Component\PropertyAccess\PropertyPath;
28+
use Symfony\Component\Validator\Constraints\File;
2729
use Symfony\Component\Validator\ConstraintViolation;
2830
use Symfony\Component\Validator\ConstraintViolationInterface;
2931

@@ -81,6 +83,7 @@ protected function getForm($name = 'name', $propertyPath = null, $dataClass = nu
8183
$config->setPropertyPath($propertyPath);
8284
$config->setCompound(true);
8385
$config->setDataMapper(new PropertyPathMapper());
86+
$config->setErrorBubbling($options['error_bubbling'] ?? false);
8487

8588
if (!$synchronized) {
8689
$config->addViewTransformer(new CallbackTransformer(
@@ -1590,4 +1593,93 @@ public function testBacktrackIfSeveralSubFormsWithSamePropertyPath()
15901593
$this->assertEquals([$this->getFormError($violation2, $grandChild2)], iterator_to_array($grandChild2->getErrors()), $grandChild2->getName().' should have an error, but has none');
15911594
$this->assertEquals([$this->getFormError($violation3, $grandChild3)], iterator_to_array($grandChild3->getErrors()), $grandChild3->getName().' should have an error, but has none');
15921595
}
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 10000 +
$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+
}
15931685
}

0 commit comments

Comments
 (0)
0