【发布时间】:2011-04-20 18:37:18
【问题描述】:
我在使用 ajax 验证来自 zend 表单的文件输入时遇到了困难,基本上我能够从普通文本输入中获取错误消息,但从文件输入中获取错误消息,即使对 html 请求进行硬编码和将它发送到 validate 函数,可能有问题,但 zend 不会验证 ajax 发送的文件输入,但它使用普通 isvalid(post) 方法进行验证。我尝试了 $.ajax 或 $post jquery 函数,但没有改变任何东西......这是不同的部分: 形式:
class Form_ContactForm extends Zend_Form
{
public function init ()
{
$this->setName("email");
$this->setMethod('post');
$mailsubject =new Zend_Form_Element_Text('mailsubject', array(
'required' => true,
'filters' => array('StringTrim')
));
$mailsubject->removeDecorator('Errors');
$this->addElement($mailsubject, 'mailsubject');
$mailattcht = new Zend_Form_Element_File('mailattcht');
$mailattcht->setDestination(APPLICATION_PATH.'/../public/mails');
$mailattcht->addValidator('Count', false, 1);
$mailattcht->addValidator('Size', false, 8000000);
$mailattcht->addValidator('Extension', false,
'jpg,png,gif,ppt,pptx,doc,docx,xls,xslx,pdf');
$mailattcht->removeDecorator('label');
//$mailattcht->removeDecorator('Errors');
$this->addElement($mailattcht, 'mailattcht');
$mailbody =new Zend_Form_Element_Textarea('mailbody', array(
'required' => true,
'filters' => array('StringTrim'),
'cols' => 40,
'rows' => 10
));
$mailbody->removeDecorator('Errors');
$this->addElement($mailbody, 'mailbody');
$sendcopy = new Zend_Form_Element_Checkbox('sendcopy');
$sendcopy->removeDecorator('label');
$this->addElement($sendcopy, 'sendcopy');
$this->addElement('submit', 'send',
array('required' => false, 'ignore' => true, 'label' => 'Send'));
/* $this->addElement('hidden', 'return', array(
'value' => Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(),
));*/
$this->setAttrib('enctype', 'multipart/form-data');
}
}
观点:
jQuery(document).ready(function()
{
$('#send').click(function(event)
{
event.preventDefault();
doValidation();
});
});
function doValidation()
{
var url = '/ceramstar/public/contact/validateform';
var data = {};
$("input").each(function(){
data[$(this).attr('name')] = $(this).val();
});
data[$("#mailbody").attr('name')] = $("#mailbody").val();
$.ajax({
type: 'POST',
url: url,
enctype: 'multipart/form-data',
data: "mailsubject=&MAX_FILE_SIZE=8388608&mailattcht=2012.srt&sendcopy=1&send=Send&mailbody=",
success: function(resp){
alert(resp);
},
dataType: "json"
});
// $.post(url,data,function(resp)
// {
//
// $('#contact').submit();
//
// if (resp == "[]"){
// window.top.location.href='<?php echo $this->baseUrl().'/contact/thankyou';?>';
// parent.$.fancybox.close();
//
//
// }
//
//
//
// },"json");
}
验证函数:
public function validateformAction()
{
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$form = new Form_ContactForm();
$form->isValidPartial($this->_getAllParams());
//$form->isValidPartial($_FILES);
$json = $form->processAjax($form->getValues());
header('Content-type: application/json');
echo Zend_Json::encode($json);
}
非常感谢您阅读它...
【问题讨论】:
标签: zend-framework jquery zend-form