Page 1 of 1

zend framework: forward not working

Posted: Sat Mar 01, 2008 4:52 pm
by raghavan20
i am having two actions, addUserFormAction and addUserAction. addUserAction has some validation on it and on errors, it tries to forward to addUserFormAction in the same controller User. but if i call directly addUserAction(http://site/addUserAction) directly without input values, it find those fields are missing but it does not forward to the addUserFormAction. i am trying to print out the request object in the controller before and after forwarding eventhough the values seem to change but the forward does not happen, i.e. it never calls the addUserFormAction method. in the code, i am using a _dump() function to print vars.

the controller code

Code: Select all

 
<?php
 
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Validate/StringLength.php';
require_once 'model/User.php';
 
 
 
class UserController extends Zend_Controller_Action{
    
    public function indexAction(){
        $this->_redirect( "user/listAll" );
    }
 
    public function addUserFormAction(){
        
        $this->_dump( "start of addUserFormAction()" );
        
        //get request pars
        $aPar = $this->getRequest()->getParams();
        
        // set form obj
        $oForm = new StdClass;
        $oForm->id = (isset( $aPar['id'] ) ? $aPar['id'] : "");
        $oForm->nickName = (isset( $aPar['nickName'] ) ? $aPar['nickName'] : "");
        $oForm->firstName = (isset( $aPar['firstName'] ) ? $aPar['firstName'] : "");
        $oForm->lastName = (isset( $aPar['lastName'] ) ? $aPar['lastName'] : "");
        $oForm->phone = (isset( $aPar['phone'] ) ? $aPar['phone'] : "");
        $oForm->email = (isset( $aPar['email'] ) ? $aPar['email'] : "");
        $oForm->description = (isset( $aPar['description'] ) ? $aPar['description'] : "");
        
        // set view
        $this->view->title = "Add User Form";
        $this->view->aError = (isset($aPar['aError']) ? $aPar['aError'] : array() );
        $this->view->oForm = $oForm;
        $this->render();
         
    }
    
    public function addUserAction(){
        
        $aPar = $this->getRequest()->getParams();
        $this->_dump( $aPar );
        
        $oValidator = new Zend_Validate_StringLength(3, 15);
        
        $aError = array();
        if( !isset($aPar['nickName']) || $oValidator->isValid( $aPar['nickName'] ) !== true ){
            $aError[] = sprintf( "Nick name should be btw %d and %d",
                        $oValidator->getMin(), $oValidator->getMax() );
        }
        
        if( !isset($aPar['firstName']) || $oValidator->isValid( $aPar['firstName'] ) !== true ){
            $aError[] = sprintf( "First name should be btw %d and %d",
                        $oValidator->getMin(), $oValidator->getMax() );
        }
            
        $oValidator->setMax( 30 );
        if( !isset($aPar['email']) || $oValidator->isValid( $aPar['email'] ) !== true ){
            $aError[] = sprintf( "Email should be btw %d and %d",
                        $oValidator->getMin(), $oValidator->getMax() );
        }       
 
        if( count($aError) > 0 ){
            print_r( $aError );
            $this->_dump( "errors found..before forward to addUserForm.." );
            $this->_dump( $this->getRequest() );
            $this->_dump( "forwarding now..." );
            $this->_forward( "addUserForm", "user", null, array( 'aError' => $aError ) );
            $this->_dump( "after forward ..." );
            $this->_dump( $this->getRequest() );
            
        }
        
        $oUser = new User;
        
        $oUser->setNickName( $aPar['nickName'] );
        $oUser->setFirstName( $aPar['firstName'] );
        $oUser->setLastName( $aPar['lastName'] );
        $oUser->setPhone( $aPar['phone'] );
        $oUser->setEmail( $aPar['email'] );
        $oUser->setDescription( $aPar['description'] );
 
        $result = $oUser->save();
        
        ##print_r($oUser);
        
        $aForwardPar = array( 'aForwardPar'=>array('addUserResult'=>$result) );
        
        ##$this->_forward( "listAll", "user", null, $aForwardPar );     
        ##$this->render( 'adduserform' );
        echo "3";
        
        
    }
    
    public function listAllAction(){    
    /*  echo "inside list all";
        
        $aPar = $this->getRequest()->getParams();
        print_r($aPar['aForwardPar']);
        
        $this->view->aPar= $aPar['aForwardPar'];
        
        ##$this->render( 'listall' );
    */  
        echo "la"; $this->render();
        
        
    }
    
    private function _dump( $var ){
        if( is_array($var) || is_object($var) || get_class($var) == "StdClass" ){
            echo "<br /><br />dump: "; echo var_export( $var, true );
        }else{
            echo "<br /><br />dump: "; echo $var;
        }
    }
    
}
 
 
?>
 

Output

Code: Select all

 
 
 
dump: array ( 'controller' => 'user', 'action' => 'addUser', 'module' => 'default', )Array ( [0] => Nick name should be btw 3 and 15 [1] => First name should be btw 3 and 15 [2] => Email should be btw 3 and 30 )
 
dump: errors found..forwarding..
 
dump: Zend_Controller_Request_Http::__set_state(array( '_requestUri' => '/accmgmt/user/addUser/', '_baseUrl' => '/accmgmt', '_basePath' => NULL, '_pathInfo' => '/user/addUser/', '_params' => array ( 'controller' => 'user', 'action' => 'addUser', 'module' => 'default', ), '_aliases' => array ( ), '_dispatched' => true, '_module' => 'default', '_moduleKey' => 'module', '_controller' => 'user', '_controllerKey' => 'controller', '_action' => 'addUser', '_actionKey' => 'action', ))
 
dump: Zend_Controller_Request_Http::__set_state(array( '_requestUri' => '/accmgmt/user/addUser/', '_baseUrl' => '/accmgmt', '_basePath' => NULL, '_pathInfo' => '/user/addUser/', '_params' => array ( 'controller' => 'user', 'action' => 'addUser', 'module' => 'default', 'aError' => array ( 0 => 'Nick name should be btw 3 and 15', 1 => 'First name should be btw 3 and 15', 2 => 'Email should be btw 3 and 30', ), ), '_aliases' => array ( ), '_dispatched' => false, '_module' => 'default', '_moduleKey' => 'module', '_controller' => 'user', '_controllerKey' => 'controller', '_action' => 'addUserForm', '_actionKey' => 'action', ))
 
dump: after forward ...
Notice: Undefined index: nickName in /home/sbharath/public_html/raghavan/accmgmt/controllers/UserController.php on line 75
 
Notice: Undefined index: firstName in /home/sbharath/public_html/raghavan/accmgmt/controllers/UserController.php on line 76
 
Notice: Undefined index: lastName in /home/sbharath/public_html/raghavan/accmgmt/controllers/UserController.php on line 77
 
Notice: Undefined index: phone in /home/sbharath/public_html/raghavan/accmgmt/controllers/UserController.php on line 78
 
Notice: Undefined index: email in /home/sbharath/public_html/raghavan/accmgmt/controllers/UserController.php on line 79
 
Notice: Undefined index: description in /home/sbharath/public_html/raghavan/accmgmt/controllers/UserController.php on line 80
 
Fatal error: Uncaught exception 'PropelException' with message 'Unable to execute INSERT statement. [wrapped: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstName' cannot be null]' in /home/sbharath/public_html/raghavan/src/accmgmt/library/propel/util/BasePeer.php:293 Stack trace: #0 /home/sbharath/public_html/raghavan/accmgmt/model/om/BaseUserPeer.php(496): BasePeer::doInsert(Object(Criteria), Object(PropelPDO)) #1 /home/sbharath/public_html/raghavan/accmgmt/model/om/BaseUser.php(658): BaseUserPeer::doInsert(Object(User), Object(PropelPDO)) #2 /home/sbharath/public_html/raghavan/accmgmt/model/om/BaseUser.php(627): BaseUser->doSave(Object(PropelPDO)) #3 /home/sbharath/public_html/raghavan/accmgmt/controllers/UserController.php(82): BaseUser->save() #4 /home/sbharath/public_html/raghavan/src/accmgmt/library/Zend/Controller/Action.php(499): UserController->addUserAction() #5 /home/sbharath/public_html/raghavan/src/accmgmt/library/Zend/Controller/Dispatcher/Standard.php(242): Zend_Controller_Action->dispat in /home/sbharath/public_html/raghavan/src/accmgmt/library/propel/util/BasePeer.php on line 293