-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Symfony version(s) affected: 3.x, 4.x
Description
According to the docs, setting the binaryFormat
option on the File
validation constraint should report all sizes in the decimal (SI-prefixed, as in kB, MB, GB etc.) format. However, the sizes will still be reported in binary (IEC, as in KiB, MiB, GiB etc.) format, completely ignoring the binaryFormat
configuration.
By setting binaryFormat=false
I'd expect the suffix
to be MB
instead of MiB
.
Since the history of megabyte
(MB) and mebibyte
(MiB) isn't already confusing enough, here's some additional reading material:
How to reproduce
Given the following php.ini
config:
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2M
and the following validation code:
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Asserts;
$INI_ERROR_MESSAGE = "Max Size: {{ limit }} {{ suffix }}";
// For the convenience of this example code we're using the `Validator` service since I can't just instantiate the `FileValidator` and call it directly as I need to inject an `ExecutionContextInterface` etc.
$validator = $this->container->get("validator");
$fakeFile = new UploadedFile(\tempnam(\sys_get_temp_dir(), 'file-validator-test-'), 'originalName', 'mime', 0, \UPLOAD_ERR_INI_SIZE);
$constraintWithBinaryFormatFalse = new File();
$constraintWithBinaryFormatFalse->binaryFormat = false;
$constraintWithBinaryFormatFalse->uploadIniSizeErrorMessage = $INI_ERROR_MESSAGE;
$constraintWithBinaryFormatTrue = new File();
$constraintWithBinaryFormatTrue->binaryFormat = true;
$constraintWithBinaryFormatTrue->uploadIniSizeErrorMessage = $INI_ERROR_MESSAGE;
$constraintWithBinaryFormatNull = new File();
$constraintWithBinaryFormatNull->binaryFormat = null;
$constraintWithBinaryFormatNull->uploadIniSizeErrorMessage = $INI_ERROR_MESSAGE;
$errorsWithBinaryFormatFalse = (string)$validator->validate($fakeFile, $constraintWithBinaryFormatFalse);
$errorsWithBinaryFormatTrue = (string)$validator->validate($fakeFile, $constraintWithBinaryFormatTrue);
$errorsWithBinaryFormatNull = (string)$validator->validate($fakeFile, $constraintWithBinaryFormatNull);
dump($errorsWithBinaryFormatFalse, $errorsWithBinaryFormatTrue, $errorsWithBinaryFormatNull);
and the results looking something like:
Possible Solution
Make the reported size format depend on the binaryFormat
option: