From 0edbbd3feaaf1fa852dd4b92c1004f7d383c23b1 Mon Sep 17 00:00:00 2001 From: Julien Fredon Date: Sun, 8 Jul 2018 23:39:51 +0200 Subject: [PATCH] Format file size in validation message according to binaryFormat option --- .../Component/Validator/Constraints/FileValidator.php | 2 +- .../Validator/Tests/Constraints/FileValidatorTest.php | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index bb96e2c9a18d..33c784b68cdd 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -58,7 +58,7 @@ public function validate($value, Constraint $constraint) $binaryFormat = $constraint->binaryFormat; } else { $limitInBytes = $iniLimitSize; - $binaryFormat = true; + $binaryFormat = null === $constraint->binaryFormat ? true : $constraint->binaryFormat; } list($sizeAsString, $limitAsString, $suffix) = $this->factorizeSizes(0, $limitInBytes, $binaryFormat); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php index add06817d0e8..6e2199ce02a1 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -455,11 +455,17 @@ public function uploadedFileErrorProvider() '{{ suffix }}' => 'bytes', ), '1'); + // access FileValidator::factorizeSizes() private method to format max file size + $reflection = new \ReflectionClass(\get_class(new FileValidator())); + $method = $reflection->getMethod('factorizeSizes'); + $method->setAccessible(true); + list($sizeAsString, $limit, $suffix) = $method->invokeArgs(new FileValidator(), array(0, UploadedFile::getMaxFilesize(), false)); + // it correctly parses the maxSize option and not only uses simple string comparison // 1000M should be bigger than the ini value $tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array( - '{{ limit }}' => UploadedFile::getMaxFilesize() / 1048576, - '{{ suffix }}' => 'MiB', + '{{ limit }}' => $limit, + '{{ suffix }}' => $suffix, ), '1000M'); // it correctly parses the maxSize option and not only uses simple string comparison