Skip to content
Browse files

merged branch craue/patch-20 (PR #4230)

Commits
-------

64101ab separate numeric value from suffix in File constraint's error message `$uploadIniSizeErrorMessage`
ff122d3 fixed tests
868d649 separate numeric values from suffixes in File constraint's error message `$maxSizeMessage`

Discussion
----------

[Validator] separate numeric values from suffixes in File validation error messages

This change allows me to locale-aware format the numbers in a form theme, i.e., to use a comma instead of a dot. If there's a better way without re-implementing the entire validator, let me know.

Such changes also allow for using a different separator than the usual space in translations.

---------------------------------------------------------------------------

by travisbot at 2012-05-08T19:14:16Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1278845) (merged f7c50098 into e54f4e46).

---------------------------------------------------------------------------

by travisbot at 2012-05-08T19:23:31Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1278940) (merged ce1cdafc into e54f4e46).

---------------------------------------------------------------------------

by r1pp3rj4ck at 2012-05-10T11:05:18Z

I don't know if there is a better way to do this, but I like the idea anyway.

---------------------------------------------------------------------------

by craue at 2012-05-11T14:18:52Z

Separated numeric values and suffixes for `$maxSizeMessage` and `$uploadIniSizeErrorMessage` now. Can't find any other relevant places (in other validators). Might be merged if accepted.

---------------------------------------------------------------------------

by travisbot at 2012-05-11T14:19:10Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1305246) (merged 438da7dd into e54f4e46).

---------------------------------------------------------------------------

by travisbot at 2012-05-11T21:22:25Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1309277) (merged 64101aba into dd0da03c).

---------------------------------------------------------------------------

by sstok at 2012-05-13T13:29:07Z

Using the NumberFormatter class would be an option, but that would also add a dependency when using Validation as stand-alone so using the {{ suffix }} is a good idea.

---------------------------------------------------------------------------

by craue at 2012-05-13T14:15:54Z

Using a NumberFormatter (if available) directly in the validator might indeed be a good option. In either case, having the numeric value separated from its suffix looks cleaner.

---------------------------------------------------------------------------

by craue at 2012-05-19T13:36:00Z

@fabpot: Would you merge this?
  • Loading branch information...
2 parents 02e02b7 + e531f5a commit 5dee24abd0caaf510daf0eed445e60b53f74402c @fabpot fabpot committed May 22, 2012
Showing with 25 additions and 15 deletions.
  1. +2 −2 Constraints/File.php
  2. +10 −6 Constraints/FileValidator.php
  3. +13 −7 Tests/Constraints/FileValidatorTest.php
View
4 Constraints/File.php
@@ -24,10 +24,10 @@ class File extends Constraint
public $mimeTypes = array();
public $notFoundMessage = 'The file could not be found.';
public $notReadableMessage = 'The file is not readable.';
- public $maxSizeMessage = 'The file is too large ({{ size }}). Allowed maximum size is {{ limit }}.';
+ public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
- public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }}.';
+ public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
public $uploadFormSizeErrorMessage = 'The file is too large.';
public $uploadPartialErrorMessage = 'The file was only partially uploaded.';
public $uploadNoFileErrorMessage = 'No file was uploaded.';
View
16 Constraints/FileValidator.php
@@ -44,7 +44,10 @@ public function validate($value, Constraint $constraint)
case UPLOAD_ERR_INI_SIZE:
$maxSize = UploadedFile::getMaxFilesize();
$maxSize = $constraint->maxSize ? min($maxSize, $constraint->maxSize) : $maxSize;
- $this->context->addViolation($constraint->uploadIniSizeErrorMessage, array('{{ limit }}' => $maxSize.' bytes'));
+ $this->context->addViolation($constraint->uploadIniSizeErrorMessage, array(
+ '{{ limit }}' => $maxSize,
+ '{{ suffix }}' => 'bytes',
+ ));
return;
case UPLOAD_ERR_FORM_SIZE:
@@ -100,23 +103,24 @@ public function validate($value, Constraint $constraint)
if (ctype_digit((string) $constraint->maxSize)) {
$size = filesize($path);
$limit = $constraint->maxSize;
- $suffix = ' bytes';
+ $suffix = 'bytes';
} elseif (preg_match('/^(\d+)k$/', $constraint->maxSize, $matches)) {
$size = round(filesize($path) / 1000, 2);
$limit = $matches[1];
- $suffix = ' kB';
+ $suffix = 'kB';
} elseif (preg_match('/^(\d+)M$/', $constraint->maxSize, $matches)) {
$size = round(filesize($path) / 1000000, 2);
$limit = $matches[1];
- $suffix = ' MB';
+ $suffix = 'MB';
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
}
if ($size > $limit) {
$this->context->addViolation($constraint->maxSizeMessage, array(
- '{{ size }}' => $size.$suffix,
- '{{ limit }}' => $limit.$suffix,
+ '{{ size }}' => $size,
+ '{{ limit }}' => $limit,
+ '{{ suffix }}' => $suffix,
'{{ file }}' => $path,
));
View
20 Tests/Constraints/FileValidatorTest.php
@@ -98,8 +98,9 @@ public function testTooLargeBytes()
$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
- '{{ limit }}' => '10 bytes',
- '{{ size }}' => '11 bytes',
+ '{{ limit }}' => '10',
+ '{{ size }}' => '11',
+ '{{ suffix }}' => 'bytes',
'{{ file }}' => $this->path,
));
@@ -118,8 +119,9 @@ public function testTooLargeKiloBytes()
$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
- '{{ limit }}' => '1 kB',
- '{{ size }}' => '1.4 kB',
+ '{{ limit }}' => '1',
+ '{{ size }}' => '1.4',
+ '{{ suffix }}' => 'kB',
'{{ file }}' => $this->path,
));
@@ -138,8 +140,9 @@ public function testTooLargeMegaBytes()
$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
- '{{ limit }}' => '1 MB',
- '{{ size }}' => '1.4 MB',
+ '{{ limit }}' => '1',
+ '{{ size }}' => '1.4',
+ '{{ suffix }}' => 'MB',
'{{ file }}' => $this->path,
));
@@ -313,7 +316,10 @@ public function uploadedFileErrorProvider()
);
if (class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
- $tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array('{{ limit }}' => UploadedFile::getMaxFilesize() . ' bytes'));
+ $tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
+ '{{ limit }}' => UploadedFile::getMaxFilesize(),
+ '{{ suffix }}' => 'bytes',
+ ));
}
return $tests;

0 comments on commit 5dee24a

Please sign in to comment.
Something went wrong with that request. Please try again.