加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 22:38:33 所属栏目:PHP教程 来源:网络整理
导读:所以我使用的是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('labe
所以我使用的是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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读