我正在使用Zend_File_Transfer进行PHP文件上传,我想为验证器添加自定义消息。
我就是这样做的:
$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();
这里没关系......问题是,如果我想更改Zend_Validate_File_Upload消息怎么办?默认情况下,文件上传验证器添加在Zend_File_Transfer_Adapter_Http类的构造函数中。
我看不到从“$ upload”实例访问文件上传验证器的方法... 删除验证器并将其添加回自定义消息不是一种选择,因为它在代码中被避免...
我在这里错过了一些东西吗?
答案 0 :(得分:3)
仅仅是为了记录,我就是这样做的:
$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"
));
答案 1 :(得分:2)
将messages
选项设置为数组,并使用验证程序中的错误消息常量来覆盖该消息。以下是电子邮件地址的示例:
$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%'"
)
));
您可以通过查看验证程序的来源或api docs来查找这些内容。
答案 2 :(得分:1)
$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.');