Thanks to visit codestin.com
Credit goes to github.com

Skip to content

File validation size format is always in binary (IEC) format, ignoring the binaryFormat option #27682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
keichinger opened this issue Jun 22, 2018 · 2 comments

Comments

@keichinger
Copy link
Contributor

keichinger commented Jun 22, 2018

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:

validator output

Possible Solution
Make the reported size format depend on the binaryFormat option:

@jfredon
Copy link
Contributor

jfredon commented Jul 8, 2018

The value 2M configured in php.ini file is 2 Mio in reality: http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes

By setting binaryFormat=false I'd expect the suffix to be MB instead of MiB.

Since FileValidator don’t print rounded value and don’t print value with more that 2 decimals, the error message will not show 2,09 MB nor 2,097152 MB but 2097152 bytes.

More generally, if the constraint is defined in binary format (KiB, MiB, GiB etc.) and the message is displayed in decimal format (kB, MB, GB), almost all the time, the unite used for display the value will be the byte due to the way how the value is formatted to be precise and readable.

I'm not sure it's more legible but it will be more standardized.
Would it be a functioning that you would find more acceptable?

@keichinger
Copy link
Contributor Author

Since FileValidator don’t print rounded value and don’t print value with more that 2 decimals, the error message will not show 2,09 MB nor 2,097152 MB but 2097152 bytes.

That's totally fine and I don't mind the rounding :)

More generally, if the constraint is defined in binary format (KiB, MiB, GiB etc.) and the message is displayed in decimal format (kB, MB, GB), almost all the time, the unite used for display the value will be the byte due to the way how the value is formatted to be precise and readable.

I agree but isn't that the reason why the binaryFormat option even exists? :)

I'm not sure it's more legible but it will be more standardized.
Would it be a functioning that you would find more acceptable?

Honestly, I'm not too sure whether I understand you correctly: Do you want to unify the unit being displayed in the error message to always show the same?

fabpot added a commit that referenced this issue Sep 4, 2018
…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
@fabpot fabpot closed this as completed Sep 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants