ZF-5898: Zend_Validate_File* not customize the error message
Description
There are validators for Zend_Validate_File that can not customize the error messages. Bellow this code that shows the customization of messages:
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->setDestination($dir);
$upload->setValidators(array(
'Size' => array('min' => 1, 'max' => 5,
'messages' => array(
Zend_Validate_File_Size::TOO_BIG => "O tamanho do arquivo '%value%' (%size%) excedeu o tamanho máximo que é de '%max%'.",
Zend_Validate_File_Size::TOO_SMALL => "O tamanho do arquivo '%value%' (%size%) não atingiu o tamanho mínimo que é de '%min%'.",
Zend_Validate_File_Size::NOT_FOUND => "O arquivo '%value%' não pôde ser encontrado."
)
),
'FilesSize' => array('min' => 1, 'max' => 5,
'messages' => array(
Zend_Validate_File_FilesSize::TOO_BIG => "A soma do tamanho dos arquivos não deveria exceder '%max%', porém foi de '%size%'.",
Zend_Validate_File_FilesSize::TOO_SMALL => "A soma do tamanho dos arquivos não deveria ser menor que'%min%', porém foi de '%size%'.",
Zend_Validate_File_FilesSize::NOT_READABLE => "Um Ou mais arquivos não puderam ser lidos."
)
),
'Count' => array('min' => 0, 'max' => 0,
'messages' => array(
Zend_Validate_File_Count::TOO_LESS => "O numero mínimo de arquivos é '%min%', porém foram fornecidos %count%",
Zend_Validate_File_Count::TOO_MUCH => "O numero máximo de arquivos é '%max%', porém foram fornecidos %count%"
)
),
'Extension' => array('jpg', 'jpeg', 'gif', 'png',
'messages' => array(
Zend_Validate_File_Extension::FALSE_EXTENSION => "A extensão do arquivo '%value%' não é válida.",
Zend_Validate_File_Extension::NOT_FOUND => "O arquivo '%value%' não pôde ser encontrado."
)
),
));
I think the problem in calling the constructor of the class validator. Below follows the suggestion of code:
Original code:
class Zend_Validate_File_Size extends Zend_Validate_Abstract
{
...
public function __construct($options)
{
...
if (isset($options['bytestring'])) {
$this->setUseByteString($options['bytestring']);
}
if (isset($options['min'])) {
$this->setMin($options['min']);
}
if (isset($options['max'])) {
$this->setMax($options['max']);
}
}
...
}
Sugestion:
class Zend_Validate_File_Size extends Zend_Validate_Abstract
{
...
public function __construct($options)
{
...
if (isset($options['bytestring'])) {
$this->setUseByteString($options['bytestring']);
}
if (isset($options['min'])) {
$this->setMin($options['min']);
}
if (isset($options['max'])) {
$this->setMax($options['max']);
}
//add this code
if (isset($options['messages'])){
$this->setMessages($options['messages']);
}
}
...
}
I have noticed that this happens in the classes: * Zend_Validate_File_Count * Zend_Validate_File_Size * Zend_Validate_File_Hash * Zend_Validate_File_Exists * Zend_Validate_File_MimeType * Zend_Validate_File_ImageSize * Zend_Validate_File_Extension
Comments
Posted by Thomas Weidner (thomas) on 2009-03-02T15:14:21.000+0000
Why should the File validators support something which all other validators do not support ?
Posted by Thomas Weidner (thomas) on 2009-03-04T13:09:00.000+0000
New feature integrated with r14224
Posted by Leandro Guimarães Fernandes (leandrogf) on 2009-03-05T09:16:36.000+0000
OK. Thanks.