Skip to content

Commit 6c0f496

Browse files
committed
bug #27970 [FileValidator] Format file size in validation message according 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 ------- 0edbbd3fea Format file size in validation message according to binaryFormat option
2 parents a73d408 + 009cd61 commit 6c0f496

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

Constraints/FileValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function validate($value, Constraint $constraint)
5858
$binaryFormat = $constraint->binaryFormat;
5959
} else {
6060
$limitInBytes = $iniLimitSize;
61-
$binaryFormat = true;
61+
$binaryFormat = null === $constraint->binaryFormat ? true : $constraint->binaryFormat;
6262
}
6363

6464
list($sizeAsString, $limitAsString, $suffix) = $this->factorizeSizes(0, $limitInBytes, $binaryFormat);

Tests/Constraints/FileValidatorTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,17 @@ public function uploadedFileErrorProvider()
455455
'{{ suffix }}' => 'bytes',
456456
), '1');
457457

458+
// access FileValidator::factorizeSizes() private method to format max file size
459+
$reflection = new \ReflectionClass(\get_class(new FileValidator()));
460+
$method = $reflection->getMethod('factorizeSizes');
461+
$method->setAccessible(true);
462+
list($sizeAsString, $limit, $suffix) = $method->invokeArgs(new FileValidator(), array(0, UploadedFile::getMaxFilesize(), false));
463+
458464
// it correctly parses the maxSize option and not only uses simple string comparison
459465
// 1000M should be bigger than the ini value
460466
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
461-
'{{ limit }}' => UploadedFile::getMaxFilesize() / 1048576,
462-
'{{ suffix }}' => 'MiB',
467+
'{{ limit }}' => $limit,
468+
'{{ suffix }}' => $suffix,
463469
), '1000M');
464470

465471
// it correctly parses the maxSize option and not only uses simple string comparison

0 commit comments

Comments
 (0)