Will creates nice web-things, builds awesome ideas, and collaborates with other creative types.

Home > Blog > Multiple Forms on a page in Zend Framework

Multiple Forms on a page in Zend Framework

by Will on Jan 9th 2012, 14:23

Zend Framework generally avoids telling you how to do things, prefering to give ways to achiving what you want, but avoiding making reccomendations.

 

That can be a weakness as well as a strength. If two forms are on one page, submitting to the same action that rendered the page, both will try and validate which looks a bit odd from user point of view and could potentialy cause application problems.

 

To avoid this, I decided to have each form submit to it's own action. This kept the main page rendering action nice and neat, separating the other logic out nicely.

 

Show Plain Text
  1. public function trackAction(){ 
  2.         $form = $this->_getCompletionForm();
  3.         $form->setAction( $this->getRequest()->getBaseUrl() . '/task/complete/task/' . $task);
  4.         # Add form to view
  5.        $this->view->formComplete = $form;
  6.    
  7.         $form = $this->_getCommentForm();
  8.         $form->setAction( $this->getRequest()->getBaseUrl() . '/task/addcomment/task/' . $task);
  9.         # Add form to view
  10.         $this->view->form = $form; 
  11. }

 

and then the actions to proccess the forms:

 

Show Plain Text
  1. public function addcommentAction(){
  2.  $task =  $this->_getParam('task');    
  3.  $form = $this->_getCommentForm();
  4.  $request = $this->getRequest();
  5.  if ($this->getRequest()->isPost() and $form->isValid($request->getPost())) {
  6.    // save comment
  7.    $redirector = $this->_helper->getHelper('Redirector');
  8.    $redirector->gotoSimple("track",  "task",null, array('mig' => $migration))
  9.   }else{
  10.    return $this->_forward('track'); // re-render the login form
  11.   }
  12. }

 

and again for the complete task form

 

Show Plain Text
  1. public function completeAction(){
  2.  $form = $this->_getCompletionForm();
  3.  $request = $this->getRequest();
  4.   if ($this->getRequest()->isPost() and $_POST['check'] = 'yep' and $form->isValid($request->getPost()))  {
  5.    /// save the information
  6.    return $this->_helper->redirector('index');
  7.    }else{
  8.     return $this->_forward('track'); // re-render the login form
  9.    }
  10. }

 

So far, so good. The forms will not show any validation however, because a new instance of the controller is created when the _forward method is used. To get round this last hurdle, we need to persist the forms in the registry. so, the get form methods look like this:

 

Show Plain Text
  1. private function _getCommentForm(){
  2.   if (!Zend_Registry::isRegistered('form_contact')){           
  3.    require_once (APPLICATION_PATH . '/forms/comment.php');
  4.    $this->_commentForm = new Form_Comment();
  5.    Zend_Registry::set('form_contact', $this->_commentForm);
  6.   }else{           
  7.    $this->_commentForm = Zend_Registry::get('form_contact');
  8.   }
  9.   return $this->_commentForm;
  10. }
  11.  
  12. private function _getCompletionForm(){
  13.  if (!Zend_Registry::isRegistered('form_complete')){
  14.   require_once (APPLICATION_PATH . '/admin/forms/Completion.php');
  15.   $this->_completeForm = new Form_Completion();
  16.   Zend_Registry::set('form_complete', $this->_completeForm);
  17.  }else{
  18.   $this->_completeForm = Zend_Registry::get('form_complete');
  19.  }
  20.  return $this->_completeForm;  
  21. }

 

And that's it! The forms submit to their own actions, forward back if not valid and display any errors (because they are the same form object, it knows to display the errors). Refrences http://framework.zend.com/manual/en/zend.registry.html

Multiple Forms on a page in Zend Framework zf

blog comments powered by Disqus