diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index e752f2a1e6502..309c989d9fc31 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -138,10 +138,12 @@ public function validate($value, Constraint $constraint) } $sizeInBytes = filesize($path); + $basename = $value instanceof UploadedFile ? $value->getClientOriginalName() : basename($path); if (0 === $sizeInBytes) { $this->context->buildViolation($constraint->disallowEmptyMessage) ->setParameter('{{ file }}', $this->formatValue($path)) + ->setParameter('{{ name }}', $this->formatValue($basename)) ->setCode(File::EMPTY_ERROR) ->addViolation(); @@ -158,6 +160,7 @@ public function validate($value, Constraint $constraint) ->setParameter('{{ size }}', $sizeAsString) ->setParameter('{{ limit }}', $limitAsString) ->setParameter('{{ suffix }}', $suffix) + ->setParameter('{{ name }}', $this->formatValue($basename)) ->setCode(File::TOO_LARGE_ERROR) ->addViolation(); @@ -189,6 +192,7 @@ public function validate($value, Constraint $constraint) ->setParameter('{{ file }}', $this->formatValue($path)) ->setParameter('{{ type }}', $this->formatValue($mime)) ->setParameter('{{ types }}', $this->formatValues($mimeTypes)) + ->setParameter('{{ name }}', $this->formatValue($basename)) ->setCode(File::INVALID_MIME_TYPE_ERROR) ->addViolation(); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php index 4cf62a6215507..9aaec3fef0485 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -176,6 +176,7 @@ public function testMaxSizeExceeded($bytesWritten, $limit, $sizeAsString, $limit ->setParameter('{{ size }}', $sizeAsString) ->setParameter('{{ suffix }}', $suffix) ->setParameter('{{ file }}', '"'.$this->path.'"') + ->setParameter('{{ name }}', '"'.basename($this->path).'"') ->setCode(File::TOO_LARGE_ERROR) ->assertRaised(); } @@ -278,6 +279,7 @@ public function testBinaryFormat($bytesWritten, $limit, $binaryFormat, $sizeAsSt ->setParameter('{{ size }}', $sizeAsString) ->setParameter('{{ suffix }}', $suffix) ->setParameter('{{ file }}', '"'.$this->path.'"') + ->setParameter('{{ name }}', '"'.basename($this->path).'"') ->setCode(File::TOO_LARGE_ERROR) ->assertRaised(); } @@ -356,6 +358,7 @@ public function testInvalidMimeType() ->setParameter('{{ type }}', '"application/pdf"') ->setParameter('{{ types }}', '"image/png", "image/jpg"') ->setParameter('{{ file }}', '"'.$this->path.'"') + ->setParameter('{{ name }}', '"'.basename($this->path).'"') ->setCode(File::INVALID_MIME_TYPE_ERROR) ->assertRaised(); } @@ -386,6 +389,7 @@ public function testInvalidWildcardMimeType() ->setParameter('{{ type }}', '"application/pdf"') ->setParameter('{{ types }}', '"image/*", "image/jpg"') ->setParameter('{{ file }}', '"'.$this->path.'"') + ->setParameter('{{ name }}', '"'.basename($this->path).'"') ->setCode(File::INVALID_MIME_TYPE_ERROR) ->assertRaised(); } @@ -402,6 +406,7 @@ public function testDisallowEmpty() $this->buildViolation('myMessage') ->setParameter('{{ file }}', '"'.$this->path.'"') + ->setParameter('{{ name }}', '"'.basename($this->path).'"') ->setCode(File::EMPTY_ERROR) ->assertRaised(); }