Zend Framework

Zend_Validate_File* not customize the error message

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.7.5
  • Fix Version/s: 1.8.0
  • Component/s: Zend_Validate_File
  • Labels:
    None

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

Activity

Hide
Thomas Weidner added a comment -

Why should the File validators support something which all other validators do not support ?

Show
Thomas Weidner added a comment - Why should the File validators support something which all other validators do not support ?
Hide
Thomas Weidner added a comment -

New feature integrated with r14224

Show
Thomas Weidner added a comment - New feature integrated with r14224
Hide
Leandro Guimarães Fernandes added a comment -

OK. Thanks.

Show
Leandro Guimarães Fernandes added a comment - OK. Thanks.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: