Hi,

I made action helper which processes my forms. I give it the form, the row
object and the success redirect url which can be modified with row data.

Pros

-thin controller
-can define redirect url in controller where I have possibility to use other
action helpers like url etc.
-Can also make AJAX functionality

Cons (at least at this point)

-Handles only Zend_Db_Table_Row objects
-AJAX responding is now compatible with my systems, what if it must be
different?

In my controller action (example):

public function editAction()
        {
                $model = new User_Model_User(); // Note! this is Model, not 
DbTable
                
                $form = $model->getForm(); // Gets User_Form_Profile class
                
                $user_row = $model->getUser($this->_getParam("id")); // Get 
user row to be
edited
                
                $this->_helper->form->process($form, $user_row, "user/:id");
                
                $this->view->form = $form;
        }

My own Action helper: 

<?php

class My_Controller_Action_Helper_Form extends
Zend_Controller_Action_Helper_Abstract
{
        public function process($form, Zend_Db_Table_Row_Abstract $row,
$redirect_url = "")
        {
                $request = $this->getRequest();
                
                // If there is a POST
                if ($request->isPost())
                {
                        // If posted by AJAX and has errors
                        if ($request->isXmlHttpRequest() &&
!$form->isValidPartial($request->getPost()))
                        {
                                $response["messages"] = $form->getMessages();
        
                                
$this->getActionController()->getHelper("json")->direct($response);
                        }
                
                        // Normal and AJAX post and no errors
                        if ($form->isValid($request->getPost()))
                        {
                                $row->setFromArray($form->getValues());
                                
                                $id = $row->save();
                        }
                
                        if ($id > 0)
                        {
                                $redirect_url = 
$this->_setRedirectUrl($redirect_url, $row);
                                
                                // If posted by AJAX
                                if ($request->isXmlHttpRequest())
                                {
                                
$this->getActionController()->getHelper("json")->direct(array("redirect" =>
$redirect_url));
                                }
                                else
                                {
                                
$this->getActionController()->getHelper("redirector")->gotoUrl($redirect_url);
                                }
                        }
                }
                else // Just populate the form with row data
                {
                        $form->populate($row->toArray());
                }
        }
        
        protected function _setRedirectUrl($url, Zend_Db_Table_Row_Abstract 
$row)
        {
                foreach ($row->toArray() as $field => $value)
                {
                        $url = str_replace(":{$field}", $value, $url);
                }

                return $url;
        }
}
-- 
View this message in context: 
http://www.nabble.com/Form-processing-by-Action-Helper--idea-tp23071046p23071046.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to