views:

183

answers:

2

I'm using Zend Framework, and am curious about how people handle form submission. Right now, I'm using something like this:

 public function editAction()
 {
    $form = my_form();
    $this->view->form = $form;

    if ($this->getRequest()->isPost() {
       $params = $this->getRequest()->getPost();

       if ($form->isValid($params) {
          // process form
       }

    }
  }

Having the page try to repost the form on refresh is just annoying, and frustrating for the user. I'd like to have all the form processing moved out of the 'edit' action, and into an 'update' action (a more RESTful approach). However, I haven't figured out a good way to take advantage of Zend_Form's built in validation decorators without using the above approach. Using the method above, error messages appear automatically if I $form->populate() a form after calling $form->isValid().

Is there any good way to persist validation messages on a form across requests (using the decorators). If not, does anyone have a solid solution for storing form validation in the session?

Thanks.

+2  A: 

After processing the form forward to another controller/action. This will send a redirect header to the client and he will be able to refresh the page without the annoying alert window.

if ($form->isValid($params) {
    // process form
    ....
    ....
    $this->_helper->Redirector->goto('another-action');
}
Goran Jurić
A: 

My way is sth like this:

/index action -> list of lets say blog posts ->update action -> updating a blog post selected in index in update action (pseudocode):

if(is_post){
  if(data_valid){
    update_data()
    redirect_to_index()
  } else {
    show_messages()
  }
}
Tomáš Fejfar
My answer is conceptual. But implementation detail is answered by Goran Jurić
Tomáš Fejfar