Format file size in validation message according to binaryFormat option

This commit is contained in:
Julien Fredon 2018-07-08 23:39:51 +02:00 committed by Julien Fredon
parent 81eb54eb78
commit 0edbbd3fea
No known key found for this signature in database
GPG Key ID: 4858D8EE72AA8334
2 changed files with 9 additions and 3 deletions

View File

@ -58,7 +58,7 @@ class FileValidator extends ConstraintValidator
$binaryFormat = $constraint->binaryFormat;
} else {
$limitInBytes = $iniLimitSize;
$binaryFormat = true;
$binaryFormat = null === $constraint->binaryFormat ? true : $constraint->binaryFormat;
}
list($sizeAsString, $limitAsString, $suffix) = $this->factorizeSizes(0, $limitInBytes, $binaryFormat);

View File

@ -455,11 +455,17 @@ abstract class FileValidatorTest extends AbstractConstraintValidatorTest
'{{ 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