Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cbaf76e

Browse filesBrowse files
committed
prevent duplicated error message for file upload limits
1 parent b60bb6e commit cbaf76e
Copy full SHA for cbaf76e

File tree

4 files changed

+85
-1
lines changed
Filter options

4 files changed

+85
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/FileType.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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;
1617
use Symfony\Component\Form\FormError;
1718
use Symfony\Component\Form\FormEvent;
@@ -171,7 +172,7 @@ private function getFileUploadError(int $errorCode)
171172
$message = strtr($messageTemplate, $messageParameters);
172173
}
173174

174-
return new FormError($message, $messageTemplate, $messageParameters);
175+
return new FileUploadError($message, $messageTemplate, $messageParameters);
175176
}
176177

177178
/**

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php
+13Lines changed: 13 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,17 @@ 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 (File::TOO_LARGE_ERROR === $violation->getCode()) {
130+
$errors = $form->getErrors();
131+
$form->clearErrors();
132+
133+
foreach ($errors as $error) {
134+
if (!$error instanceof FileUploadError) {
135+
$form->addError($error);
136+
}
137+
}
138+
}
139+
127140
$scope->addError(new FormError(
128141
$violation->getMessage(),
129142
$violation->getMessageTemplate(),
+19Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php
+51Lines changed: 51 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

@@ -1590,4 +1592,53 @@ public function testBacktrackIfSeveralSubFormsWithSamePropertyPath()
15901592
$this->assertEquals([$this->getFormError($violation2, $grandChild2)], iterator_to_array($grandChild2->getErrors()), $grandChild2->getName().' should have an error, but has none');
15911593
$this->assertEquals([$this->getFormError($violation3, $grandChild3)], iterator_to_array($grandChild3->getErrors()), $grandChild3->getName().' should have an error, but has none');
15921594
}
1595+
1596+
public function testFileUploadErrorIsNotRemovedIfNoFileSizeConstraintViolationWasRaised()
1597+
{
1598+
$form = $this->getForm('form');
1599+
$form->addError(new FileUploadError(
1600+
'The file is too large. Allowed maximum size is 2 MB.',
1601+
'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.',
1602+
[
1603+
'{{ limit }}' => '2',
1604+
'{{ suffix }}' => 'MB',
1605+
]
1606+
));
1607+
1608+
$this->mapper->mapViolation($this->getConstraintViolation('data'), $form);
1609+
1610+
$this->assertCount(2, $form->getErrors());
1611+
}
1612+
1613+
public function testFileUploadErrorIsRemovedIfFileSizeConstraintViolationWasRaised()
1614+
{
1615+
$form = $this->getForm('form');
1616+
$form->addError(new FileUploadError(
1617+
'The file is too large. Allowed maximum size is 2 MB.',
1618+
'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.',
1619+
[
1620+
'{{ limit }}' => '2',
1621+
'{{ suffix }}' => 'MB',
1622+
]
1623+
));
1624+
1625+
$violation = new ConstraintViolation(
1626+
'The file is too large (3 MB). Allowed maximum size is 2 MB.',
1627+
'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.',
1628+
[
1629+
'{{ limit }}' => '2',
1630+
'{{ size }}' => '3',
1631+
'{{ suffix }}' => 'MB',
1632+
],
1633+
'data',
1634+
'',
1635+
null,
1636+
null,
1637+
File::TOO_LARGE_ERROR
1638+
);
1639+
$this->mapper->mapViolation($this->getConstraintViolation('data'), $form);
1640+
$this->mapper->mapViolation($violation, $form);
1641+
1642+
$this->assertCount(2, $form->getErrors());
1643+
}
15931644
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.