Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitcbaf76e

Browse files
committed
prevent duplicated error message for file upload limits
1 parentb60bb6e commitcbaf76e

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\Form\Extension\Core\Type;
1313

1414
useSymfony\Component\Form\AbstractType;
15+
useSymfony\Component\Form\FileUploadError;
1516
useSymfony\Component\Form\FormBuilderInterface;
1617
useSymfony\Component\Form\FormError;
1718
useSymfony\Component\Form\FormEvent;
@@ -171,7 +172,7 @@ private function getFileUploadError(int $errorCode)
171172
$message =strtr($messageTemplate,$messageParameters);
172173
}
173174

174-
returnnewFormError($message,$messageTemplate,$messageParameters);
175+
returnnewFileUploadError($message,$messageTemplate,$messageParameters);
175176
}
176177

177178
/**

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

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

1212
namespaceSymfony\Component\Form\Extension\Validator\ViolationMapper;
1313

14+
useSymfony\Component\Form\FileUploadError;
1415
useSymfony\Component\Form\FormError;
1516
useSymfony\Component\Form\FormInterface;
1617
useSymfony\Component\Form\Util\InheritDataAwareIterator;
1718
useSymfony\Component\PropertyAccess\PropertyPathBuilder;
1819
useSymfony\Component\PropertyAccess\PropertyPathIterator;
1920
useSymfony\Component\PropertyAccess\PropertyPathIteratorInterface;
21+
useSymfony\Component\Validator\Constraints\File;
2022
useSymfony\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 ($errorsas$error) {
134+
if (!$errorinstanceof FileUploadError) {
135+
$form->addError($error);
136+
}
137+
}
138+
}
139+
127140
$scope->addError(newFormError(
128141
$violation->getMessage(),
129142
$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+
namespaceSymfony\Component\Form;
13+
14+
/**
15+
* @internal
16+
*/
17+
class FileUploadErrorextends FormError
18+
{
19+
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
useSymfony\Component\Form\Exception\TransformationFailedException;
1919
useSymfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
2020
useSymfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;
21+
useSymfony\Component\Form\FileUploadError;
2122
useSymfony\Component\Form\Form;
2223
useSymfony\Component\Form\FormConfigBuilder;
2324
useSymfony\Component\Form\FormError;
2425
useSymfony\Component\Form\FormInterface;
2526
useSymfony\Component\Form\Tests\Extension\Validator\ViolationMapper\Fixtures\Issue;
2627
useSymfony\Component\PropertyAccess\PropertyPath;
28+
useSymfony\Component\Validator\Constraints\File;
2729
useSymfony\Component\Validator\ConstraintViolation;
2830
useSymfony\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+
publicfunctiontestFileUploadErrorIsNotRemovedIfNoFileSizeConstraintViolationWasRaised()
1597+
{
1598+
$form =$this->getForm('form');
1599+
$form->addError(newFileUploadError(
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+
publicfunctiontestFileUploadErrorIsRemovedIfFileSizeConstraintViolationWasRaised()
1614+
{
1615+
$form =$this->getForm('form');
1616+
$form->addError(newFileUploadError(
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 =newConstraintViolation(
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)

[8]ページ先頭

©2009-2025 Movatter.jp