How to use Zend Framework Form Hash (token) with AJAX

前端 未结 6 724

I have included Zend_Form_Element_Hash into a form multiplecheckbox form. I have jQuery set to fire off an AJAX request when a checkbox is clicked, I pass the token with thi

6条回答
  •  無奈伤痛
    2020-12-28 09:12

    If you want to use form validator in ajax side use following code :

    Myform.php

    class Application_Form_Myform extends Zend_Form
    { 
        # init function & ... 
        public function generateform($nohash = false)
        { 
            # Some elements
            if(!$nohash)
            {
               $temp_csrf = new Zend_Session_Namespace('temp_csrf'); 
               $my_hash = new Zend_Form_Element_Hash ( 'my_hash' );
               $this->addElement ( $my_hash , 'my_hash');  
               $temp_csrf->hash = $my_hash->getHash();
            }
            # Some other elements
        }
    }
    

    AjaxController.php

    class AjaxController extends Zend_Controller_Action
    { 
        // init ... 
        public function validateAction()
        { 
             # ... 
             $temp_csrf = new Zend_Session_Namespace('temp_csrf');
             if($temp_csrf->hash == $params['received_hash_from_client'])
             {
                 $Myform     = new Application_Form_Myform(); 
                 $Myform->generateform(true);
                 if($AF_Bill->isValid($params))
                 {
                     # Form data is valid
                 }else{
                     # Form invalid
                 }
             }else{
                 # Received hash from client is not valid
             }
             # ... 
        }
    }
    

提交回复
热议问题