Skip to content
Permalink
Browse files

bug #27970 [FileValidator] Format file size in validation message acc…

…ording to binaryFormat option (jfredon)

This PR was merged into the 2.8 branch.

Discussion
----------

[FileValidator] Format file size in validation message according to binaryFormat option

| Q             | A
| ------------- | ---
| Branch?       | 2.8 up to master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #27682
| License       | MIT
| Doc PR        |

The binaryFormat option of the constraint is not taken into account if the maxsize limit is defined by the php configuration files.
This patch correct this inconsistent behavior.

If the binaryOption is not set, the unit of measurement used remains in binary because it’s the unit used in php configuration files.

Commits
-------

0edbbd3 Format file size in validation message according to binaryFormat option
  • Loading branch information...
fabpot committed Sep 4, 2018
2 parents e7d325f + 0edbbd3 commit a1596382338f0cd9057bd911a4e71599798d1e82
@@ -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);
@@ -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

0 comments on commit a159638

Please sign in to comment.
You can’t perform that action at this time.