0
我从使用Ajax Zend的形式验证文件输入有困难的时候,基本上我能够从普通文本输入得到的错误消息,但从来没有从文件输入,甚至如果对html请求进行硬编码并将其发送到验证函数,则可能是错误的,但是zend不会验证由ajax发送的文件输入,但它会通过正常的isvalid(post)方法执行。我想无论是$阿贾克斯或$后jQuery的功能,但不会改变任何东西......这里有不同的部分: 形式:Zend的文件输入形式AJAX验证
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);
}
感谢很多甚至只是读它...
类似http://stackoverflow.com/questions/5597581/zend-form-jquery-fileuploaderrorinisize – Vika 2011-04-20 19:05:39