2011-04-20 80 views
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); 
    } 

感謝很多甚至只是讀它...

+0

類似http://stackoverflow.com/questions/5597581/zend-form-jquery-fileuploaderrorinisize – Vika 2011-04-20 19:05:39

回答

1

您不能從網絡讀取le輸入字段與jQuery/JavaScript,這是由於js安全限制。如果我沒有記錯,任何從文件輸入元素讀取都會返回一個空字符串。

的$形式 - > isValidPartial()方法將不執行驗證作爲請求PARAM mailattcht是空的。

你也可以不通過AJAX進行文件上傳,如果這是你的最終目的。

親切的問候

加里