php – 如何设置自定义错误消息zend表单元素文件?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何设置自定义错误消息zend表单元素文件?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我使用的是Zend,我有一个带有Zend_Form_Element_File和三个验证器的Zend表单:
1. setrequired
2.扩展
3.尺寸
$this->browse = new Zend_Form_Element_File('Browse');
 $this->browse->setrequired(false)->removeDecorator('errors')->removeDecorator('label')
->addValidator('Extension',true,'pDF')->addValidator('Size',false,2000000);

我想为这些验证器设置自定义错误消息,但不知道如何.

我想设置自定义错误消息的原因是因为我有一个自定义装饰器,当表单与isValid()无效时我抓住所有错误并将其显示在表单的顶部.我在表单中捕获错误方法是getErrors().

我也尝试过:http://www.mail-archive.com/fw-general@lists.zend.com/msg25779.html
通过做:

$validator = new Zend_Validate_File_Upload();
 $validator->setMessages(array('fileUploadErrorNoFile' => 'Upload an image!''));

和做

$this->browse->addValidator($validator);

有帮助吗?

这是我用来设置自定义验证器消息的方式.
$file = new Zend_Form_Element_File('file');
$file->setLabel('File Label')
     ->setMaxFileSize('512000')
     ->addValidator('Count',1)
     ->addValidator('Size',512000)
     ->addValidator('Extension','jpg,jPEg,png,gif');

$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,png and gif are Allowed.');

这里有一些可能对理解自定义验证器消息有用的链接.

http://framework.zend.com/manual/en/zend.validate.messages.html

Zend Framework Custom Validation Class Error Message

Can’t set custom validator messages in Zend_Form

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何设置自定义错误消息zend表单元素文件?全部内容,希望文章能够帮你解决php – 如何设置自定义错误消息zend表单元素文件?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。