RE: Correct way to handle ajax forms
In my form I also do the same thing to switch the open / closing with an if statement for the add or edit that's being called. -Original Message- From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf Of lowpass Sent: Tuesday, January 01, 2013 4:32 PM To: cake-php@googlegroups.com Subject: Re: Correct way to handle ajax forms I haven't done much AJAX stuff with 2.2.x yet but that's basically what I've always been doing. Generally, my add and edit templates just include the form.ctp element. In that I have a switch($this->params['action']) to decide how to open/close the form. But the point is to be able to render and return just the form portion of a view. On Tue, Jan 1, 2013 at 1:45 PM, Advantage+ wrote: > Just wondering what is the correct way to handle processing forms > submitted via ajax / json response? > > > > What I have been doing is converting my add / edit forms into a single > element and placing them into the view/add/edit where needed so on > error all I need to do is return the form element or on success > whatever is needed success message. > > > > In the controller : > > > > if ( $this->Model->save($this->request->data, true)) { > > > > $response = array( > > 'status' => true, > > 'html' => > $this->render($this->autoElement('added_block'))->body(), > > 'message' => 'Saved!'); > > } else { > > > > $this->set('errors', $this->Model->validationErrors); > > $response = array( > > 'status' => false, > > 'html' => > $this->render($this->autoElement('form'))->body(), > > 'message' => 'Please > correct the following errors and try again!' ); > > } > > $this->_ajaxReturn( $response ); //simply encodes $response array to > json that gets passed to the js > > > > This way the js can act according to the response status and has all > the HTML info ready to drop into my or where ever and has all > the Cake inputs with errors. Is this going about it wrong? Or is there an easier way? > > Just curious how others are approaching this. > > > > Thanks, > > > > Dave > > -- > Like Us on FaceBook https://www.facebook.com/CakePHP Find us on > Twitter http://twitter.com/CakePHP > > --- > You received this message because you are subscribed to the Google > Groups "CakePHP" group. > To post to this group, send email to cake-php@googlegroups.com. > To unsubscribe from this group, send email to > cake-php+unsubscr...@googlegroups.com. > Visit this group at http://groups.google.com/group/cake-php?hl=en. > > -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php?hl=en. -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php?hl=en.
Re: Correct way to handle ajax forms
I haven't done much AJAX stuff with 2.2.x yet but that's basically what I've always been doing. Generally, my add and edit templates just include the form.ctp element. In that I have a switch($this->params['action']) to decide how to open/close the form. But the point is to be able to render and return just the form portion of a view. On Tue, Jan 1, 2013 at 1:45 PM, Advantage+ wrote: > Just wondering what is the correct way to handle processing forms submitted > via ajax / json response? > > > > What I have been doing is converting my add / edit forms into a single > element and placing them into the view/add/edit where needed so on error all > I need to do is return the form element or on success whatever is needed > success message. > > > > In the controller : > > > > if ( $this->Model->save($this->request->data, true)) { > > > > $response = array( > > 'status' => true, > > 'html' => > $this->render($this->autoElement('added_block'))->body(), > > 'message' => 'Saved!'); > > } else { > > > > $this->set('errors', $this->Model->validationErrors); > > $response = array( > > 'status' => false, > > 'html' => > $this->render($this->autoElement('form'))->body(), > > 'message' => 'Please > correct the following errors and try again!' ); > > } > > $this->_ajaxReturn( $response ); //simply encodes $response array to json > that gets passed to the js > > > > This way the js can act according to the response status and has all the > HTML info ready to drop into my or where ever and has all the Cake > inputs with errors. Is this going about it wrong? Or is there an easier way? > > Just curious how others are approaching this. > > > > Thanks, > > > > Dave > > -- > Like Us on FaceBook https://www.facebook.com/CakePHP > Find us on Twitter http://twitter.com/CakePHP > > --- > You received this message because you are subscribed to the Google Groups > "CakePHP" group. > To post to this group, send email to cake-php@googlegroups.com. > To unsubscribe from this group, send email to > cake-php+unsubscr...@googlegroups.com. > Visit this group at http://groups.google.com/group/cake-php?hl=en. > > -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php?hl=en.
Correct way to handle ajax forms
Just wondering what is the correct way to handle processing forms submitted via ajax / json response? What I have been doing is converting my add / edit forms into a single element and placing them into the view/add/edit where needed so on error all I need to do is return the form element or on success whatever is needed success message. In the controller : if ( $this->Model->save($this->request->data, true)) { $response = array( 'status' => true, 'html' => $this->render($this->autoElement('added_block'))->body(), 'message' => 'Saved!'); } else { $this->set('errors', $this->Model->validationErrors); $response = array( 'status' => false, 'html' => $this->render($this->autoElement('form'))->body(), 'message' => 'Please correct the following errors and try again!' ); } $this->_ajaxReturn( $response ); //simply encodes $response array to json that gets passed to the js This way the js can act according to the response status and has all the HTML info ready to drop into my or where ever and has all the Cake inputs with errors. Is this going about it wrong? Or is there an easier way? Just curious how others are approaching this. Thanks, Dave -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php?hl=en.