0
votes

I have a form say:

class Application_Form_UserDetails extends Zend_Form
{

public function init()
{

    $pswd = new Zend_Form_Element_Password('password');
    $pswd->setLabel('New password:');
    $pswd->setAttrib('size', 25);
    $pswd->setRequired(false);
    $pswd->addValidator('StringLength', false, array(4,15));
    $pswd->addErrorMessage('Wron password');
}
}

In my user details controller class I have:

  class UserDetailsController extends Zend_Controller_Action {



public function editAction()
{

    $userId = $this->userInfo->id;

    $DbTableUsers = new Application_Model_DbTable_User;

    $obj = $DbTableUsers->getUserDetails($userId);

    $this->view->formUser = new $this->_UserDetails_form_class;
    $this->view->formCompany = new $this->_CompanyDetails_form_class;

    if ($obj) {

        $this->view->formUser->populate($obj);
    }

    $url = $this->view->url(array('action' => 'update-user-details'));
    $this->view->formUser->setAction($url);

    }







public function updateUserDetailsAction()
{


    $formUser = new $this->_UserDetails_form_class;


    if ($formUser->isValid($this->getRequest()->getPost())) {



    }
    else {
       //validation failed

        $formUser->markAsError();

        $this->view->formUser = $formUser;
        $this->_helper->redirector('edit', 'user-details');


    }


}



  }

The first time Edit action is called the form built and displayed. User fills the form and sends it (updateUserDetailsAction is called). In updateUserDetailsAction, on validation failure I mark the form as having errors and want to display the form with error messages that I previously set in updateUserDetailsAction class.

Then I redirect: $this->_helper->redirector('edit', 'user-details');

in order to display the same form but with errors for the user to re-enter correct values. The problem is I don't know how to let know the edit action that the form must display validation errors?

On $this->_helper->redirector('edit', 'user-details'); the form is redisplayed as a new form with cleared erros but I need them displayed.

Do I do this the correct way?

regards Tom

1

1 Answers

0
votes

Problem comes from the fact that you are redirecting and in each method you are creating a new instance of the form, that means the form class is loosing its state - data you injected from the request and any other values passed to this object.

Combine editAction and updateUserDetailsAction into one method:

...
$formUser = new Form();
// populate the form from the model
if ($this->getRequest()->isPost()) {
    if ($formUser->isValid($this->getRequest()->getPost())) {
        // update the model
    }
}
...

and have the form being submitted to the edit action. This will simplify your code and remove code duplication.

If you just wan to fix your code you can instantiate the form object in the init() method of your controller as set it as a property of your controller. This will way you will reuse same instance after redirection. I still think that solution above is much more compact and easier to understand for someone else.