ZF-4394: Zend_Validate_File_Size: Error message template variables

Description

In Zend_Validate_File_Size I reckon it'd be great if _throw was setting _min and _max to the respective values of the getMin() and getMax() methods. Since both methods call _toByteString it would result in friendlier error messages when they make use of the %min and %max placeholders.

I would also suggest breaking the loop in _toByteString when $size >= 1024 (instead of $size > 1024), 1MB is imo more user friendly than 1024kB.

Also in isValid() there is $this->_setValue($size) which sets %value to a file size. It is a bit confusing since the default error messages more or less treat %value as a file name. Even though it's overwritten in _throw when $files is set it might surprise some people that use this validator manually. (Zend_Validate_File_FileSize and Zend_Validate_File_ImageSize also do something similar).

Last suggestion, maybe a %size placeholder set to the actual size of the tested file could be useful to some people.

Comments

Fixed with r11536

Hi Thomas, thanks for implementing my suggestions; you left one out though and because you didn't comment on it I reopen the issue in case it wasn't intentional.

Here is the message currently output by the validator: "Maximum allowed size for file 'test.doc' is '1048576' but '4615933' detected."

I'd say the following message would be much more friendly to the average user: "Maximum allowed size for file 'test.doc' is '1MB' but '4.4MB' detected"

Replacing the numeric values for _min, _max and _size by their string counterparts could be achieved in _throw, systematically or conditionally depending on a Boolean value given as a parameter of the constructor.

This was not implemented for clearification of value. When both values are nearly the same the difference would not be visible.

When you have the limit with 1048576 and the file with 1048577 the resulting message would be "Maximum allowed size for file 'test.doc' is '1MB' but '1MB' detected."

Not really what we want :-)

I see your point, but I'm not sure the benefit of displaying the full numeric value outweighs the disadvantages.

First because for most non-technical people 1048576 is nothing more than a scary 7 figures number.

Also because a lot of programmers will use custom messages which won't necessarily display both values, in which case they won't have this issue and will probably enjoy being able to customize the format of their values. The Boolean parameter for the constructor could be a good way to implement this, leaving it to the programmers to decide what they want.

I can also think of two workarounds to the issue you mentioned when both values are displayed:

  • Changing the precision until the values differ, for example: "Maximum allowed size for file 'test.doc' is '1MB' but '1.001MB' detected." I believe it is an acceptable solution assuming that a difference of only a few bytes isn't the most common case.

  • More simple to implement: changing the wording so that it suggests that the displayed value is indicative and not a precise value. "Maximum allowed size for file 'test.doc' is '1MB' but slightly more than '1MB' detected."

Fixed with r11565