开发者

How do I set custom messages for Zend_File_Transfer_Adapter_Http validators

I'm using Zend_File_Transfer for PHP file uploads and I want to add custom messages for validators.

This is how I do it:

$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator('Size', false, array(
    'max' => $userLimits->photos->max_size * 1024 * 1024,
    'messages' => "'%value%' adlı dosya en fazla '%max%' olmalıdır"));

$upload->addValidator('Extension', false, array(
    'case' => 'jpg,jpeg', 
    'messages' => "'%value%' jpg veya jpeg formatında olmalıdır"));

if (!$upload->isValid()) {
    throw new Zf_Model_Exception('Hata: '.implode('<br>', $upload->getMessages()));
}

$files = $upload->getFileInfo();

It's ok upto here... The problem is, what if I want to change the Zend_Validate_File_Upload messages? File upload validator is added in the contructor开发者_如何学C of the Zend_File_Transfer_Adapter_Http class by default.

I couldn't see a way to access the file upload validator from "$upload" instance... Removing the validator and adding it back with custom messages is not an option since it's avoided in the code...

So am I missing something here?


Just for the records, that's how I did it:

$upload = new Zend_File_Transfer_Adapter_Http();

$upload->getValidator('Upload')->setMessages(array(
    Zend_Validate_File_Upload::INI_SIZE       => "'%value%' adlı dosya çok büyük",
    Zend_Validate_File_Upload::FORM_SIZE      => "'%value%' adlı dosya çok büyük",
    Zend_Validate_File_Upload::PARTIAL        => "'%value%' adlı dosya yüklenemedi",
    Zend_Validate_File_Upload::NO_FILE        => "'%value%' adlı dosya yüklenemedi",
    Zend_Validate_File_Upload::NO_TMP_DIR     => "'%value%' adlı dosya yüklenemedi",
    Zend_Validate_File_Upload::CANT_WRITE     => "'%value%' adlı dosya yüklenemedi",
    Zend_Validate_File_Upload::EXTENSION      => "'%value%' adlı dosya yüklenemedi",
    Zend_Validate_File_Upload::ATTACK         => "'%value%' adlı dosya yüklenemedi",
    Zend_Validate_File_Upload::FILE_NOT_FOUND => "'%value%' adlı dosya bulunamadı",
    Zend_Validate_File_Upload::UNKNOWN        => "'%value%' adlı dosya yüklenemedi"
));


Set the messages option to an array, and use the error message constants from the validator to override the message. Here is an example for email address:

$element->addValidator('EmailAddress', false, array(
    'messages' => array(
        Zend_Validate_EmailAddress::INVALID_FORMAT => "'%value%' is not a valid email address. Example: you@yourdomain.com",
        Zend_Validate_EmailAddress::INVALID_HOSTNAME => "'%hostname%' is not a valid hostname for email address '%value%'"
    )
));

You can find those by looking at the source for the validator, or from the api docs.


$file->getValidator('Count')->setMessage('You can upload only one file');
$file->getValidator('Size')->setMessage('Your file size cannot upload file size limit of 512 kb');
$file->getValidator('Extension')->setMessage('Invalid file extension, only valid image with file format jpg, jpeg, png and gif are allowed.');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜