开发者

HowTo: Zend_Form processing via ajax

开发者 https://www.devze.com 2023-04-11 15:18 出处:网络
i\'m digging more and more into Zend_Framework, but finding good resources isn\'t all too easy, as a lot is coming from older versions. So i\'m hoping to find some input from here.

i'm digging more and more into Zend_Framework, but finding good resources isn't all too easy, as a lot is coming from older versions. So i'm hoping to find some input from here.

I'm looking for the right way to ajaxify my Zend_Forms. Sometimes i want a form to be called via ajax and then obviously process it via ajax, too. Calling the ajax form is working fine, but im struggling a little bit with the processing.

Currently my ac开发者_开发技巧tion looks like this:

// asset/add
public function addAction() {
  $form = new Application_Form_Asset();
  if($this->getRequest()->isPost()) {
    if($form->isValid($this->_getAllParams())) {
      // Process to database using either Flashmessenger and redirector or view->message for success output
    }
  }
  $this->view->form = $form;
}

Now i don't really understand by how far i need to change this code. Basically what i want to achieve is the following:

  1. Call the form via ajax (that i can do, ajaxContext and such, no problem)
  2. Process the form via ajax, too (working, too, jQuery, but the processing result is not what i want
  3. The result should be a json from the object stored to the database

And the connection from point 2-3 is exactly where i have my problems finding resources for on the internet.

Guessed Solution: My guess is, that i need to rearrange my Controllers and Form-handling in a way that i don't handle everything inside addAction() but to have this and processaddAction() where addActions only task is to display the form and processadd does the validation and insertion to the database. But even with this model i don't really see how i can do the json output.

I'm simply stuck a little in my thought. Some "Zendwords" i should look up would probably be enough for me to find out what i want to achieve, but if someone wants to give me a working example, i wouldn't say no ;)

Thanks in advance!


I'm not sure if this suits exactly your needs, but the following tutorial is quite clear:

http://www.zendcasts.com/ajaxify-your-zend_form-validation-with-jquery/2010/04/

I'm using the ideas contained there to validate the fields through Ajax (as you say, using a different validateAction), both interactively (when they lose the focus in the browser) and before submitting.

Hope that helps,


I did it by adding a button (not submit type) and a jquery click event on that button. Validation was hadeled by validation plugin for jquery.

  1. The Ajax call will point to the controller/action (processaddAction) that will proccess the data.

  2. Then get data and json_encode it (return $json)

    public function processaddAction(){ //do the proccessing return json_encode($data); }

  3. Your Ajax success function will catch the json

    $.ajax({ url: "/controller/processaddAction", type: 'POST', data: 'data='+ [POST DATA], async: false, success: function(theJson){

    } });

The form was only generated by Zend-FW.

0

精彩评论

暂无评论...
验证码 换一张
取 消