Sol*_*gon 7 php validation zend-framework element file
所以我使用的是Zend,我有一个带有Zend_Form_Element_File和三个验证器的Zend表单:1.setRequired 2. Extension 3. Size
$this->browse = new Zend_Form_Element_File('Browse');
$this->browse->setRequired(false)->removeDecorator('errors')->removeDecorator('label')
->addValidator('Extension', true, 'pdf')->addValidator('Size', false, 2000000);
Run Code Online (Sandbox Code Playgroud)
我想为这些验证器设置自定义错误消息,但不知道如何.
我想设置自定义错误消息的原因是因为我有一个自定义装饰器,当表单与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!''));
Run Code Online (Sandbox Code Playgroud)
并做
$this->browse->addValidator($validator);
Run Code Online (Sandbox Code Playgroud)
有帮助吗?
Ibr*_*mar 20
这是我用来设置自定义验证器消息的方式.
$file = new Zend_Form_Element_File('file');
$file->setLabel('File Label')
->setMaxFileSize('512000')
->addValidator('Count', true, 1)
->addValidator('Size', true, 512000)
->addValidator('Extension', true, '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, jpeg, png and gif are allowed.');
Run Code Online (Sandbox Code Playgroud)
这里有一些可能对理解自定义验证器消息有用的链接.
http://framework.zend.com/manual/en/zend.validate.messages.html