Skip to content

Commit

Permalink
prevent duplicated error message for file upload limits
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Nov 19, 2020
1 parent b60bb6e commit cbaf76e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/FileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FileUploadError;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
Expand Down Expand Up @@ -171,7 +172,7 @@ private function getFileUploadError(int $errorCode)
$message = strtr($messageTemplate, $messageParameters);
}

return new FormError($message, $messageTemplate, $messageParameters);
return new FileUploadError($message, $messageTemplate, $messageParameters);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

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

use Symfony\Component\Form\FileUploadError;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Util\InheritDataAwareIterator;
use Symfony\Component\PropertyAccess\PropertyPathBuilder;
use Symfony\Component\PropertyAccess\PropertyPathIterator;
use Symfony\Component\PropertyAccess\PropertyPathIteratorInterface;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\ConstraintViolation;

/**
Expand Down Expand Up @@ -124,6 +126,17 @@ public function mapViolation(ConstraintViolation $violation, FormInterface $form

// Only add the error if the form is synchronized
if ($this->acceptsErrors($scope)) {
if (File::TOO_LARGE_ERROR === $violation->getCode()) {
$errors = $form->getErrors();
$form->clearErrors();

foreach ($errors as $error) {
if (!$error instanceof FileUploadError) {
$form->addError($error);
}
}
}

$scope->addError(new FormError(
$violation->getMessage(),
$violation->getMessageTemplate(),
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Form/FileUploadError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form;

/**
* @internal
*/
class FileUploadError extends FormError
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;
use Symfony\Component\Form\FileUploadError;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigBuilder;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Tests\Extension\Validator\ViolationMapper\Fixtures\Issue;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationInterface;

Expand Down Expand Up @@ -1590,4 +1592,53 @@ public function testBacktrackIfSeveralSubFormsWithSamePropertyPath()
$this->assertEquals([$this->getFormError($violation2, $grandChild2)], iterator_to_array($grandChild2->getErrors()), $grandChild2->getName().' should have an error, but has none');
$this->assertEquals([$this->getFormError($violation3, $grandChild3)], iterator_to_array($grandChild3->getErrors()), $grandChild3->getName().' should have an error, but has none');
}

public function testFileUploadErrorIsNotRemovedIfNoFileSizeConstraintViolationWasRaised()
{
$form = $this->getForm('form');
$form->addError(new FileUploadError(
'The file is too large. Allowed maximum size is 2 MB.',
'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.',
[
'{{ limit }}' => '2',
'{{ suffix }}' => 'MB',
]
));

$this->mapper->mapViolation($this->getConstraintViolation('data'), $form);

$this->assertCount(2, $form->getErrors());
}

public function testFileUploadErrorIsRemovedIfFileSizeConstraintViolationWasRaised()
{
$form = $this->getForm('form');
$form->addError(new FileUploadError(
'The file is too large. Allowed maximum size is 2 MB.',
'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.',
[
'{{ limit }}' => '2',
'{{ suffix }}' => 'MB',
]
));

$violation = new ConstraintViolation(
'The file is too large (3 MB). Allowed maximum size is 2 MB.',
'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.',
[
'{{ limit }}' => '2',
'{{ size }}' => '3',
'{{ suffix }}' => 'MB',
],
'data',
'',
null,
null,
File::TOO_LARGE_ERROR
);
$this->mapper->mapViolation($this->getConstraintViolation('data'), $form);
$this->mapper->mapViolation($violation, $form);

$this->assertCount(2, $form->getErrors());
}
}

0 comments on commit cbaf76e

Please sign in to comment.