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

Skip to content

Commit e34d9a3

Browse files
committed
Format file size in validation message according to binaryFormat option
1 parent 13a4003 commit e34d9a3

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Symfony/Component/Validator/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);

src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function setUp()
3636
{
3737
parent::setUp();
3838

39-
$this->path = sys_get_temp_dir().DIRECTORY_SEPARATOR.'FileValidatorTest';
39+
$this->path = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'FileValidatorTest';
4040
$this->file = fopen($this->path, 'w');
4141
fwrite($this->file, ' ', 1);
4242
}
@@ -57,6 +57,16 @@ protected function tearDown()
5757
$this->file = null;
5858
}
5959

60+
// Allows the invocation of a private method
61+
public function invokeMethod($object, $methodName, $params = array())
62+
{
63+
$reflection = new \ReflectionClass(get_class($object));
64+
$method = $reflection->getMethod($methodName);
65+
$method->setAccessible(true);
66+
67+
return $method->invokeArgs($object, $params);
68+
}
69+
6070
public function testNullIsValid()
6171
{
6272
$this->validator->validate(null, new File());
@@ -455,11 +465,15 @@ public function uploadedFileErrorProvider()
455465
'{{ suffix }}' => 'bytes',
456466
), '1');
457467

468+
list($sizeAsString, $limit, $suffix) = $this->invokeMethod(
469+
new FileValidator(), 'factorizeSizes', array(0, UploadedFile::getMaxFilesize(), false)
470+
);
471+
458472
// it correctly parses the maxSize option and not only uses simple string comparison
459473
// 1000M should be bigger than the ini value
460474
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
461-
'{{ limit }}' => UploadedFile::getMaxFilesize() / 1048576,
462-
'{{ suffix }}' => 'MiB',
475+
'{{ limit }}' => $limit,
476+
'{{ suffix }}' => $suffix,
463477
), '1000M');
464478

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

0 commit comments

Comments
 (0)