Strageties for lost view vars when re-rendering an invalid form ?

1 message Options
Embed this post
Permalink
J DeBord

Strageties for lost view vars when re-rendering an invalid form ?

Reply Threaded More More options
Print post
Permalink
Problem: When rendering an action's view script from a different action, the view variables that normally would exist are never initialized.

For example, indexAction creates a form it's view script and at least one other variable for the view:
public function indexAction()
    {
        $this->view->runs = array();

        $contactForm = $this->_getContactForm();
        $this->view->contactForm = $contactForm;

        $dbAdapter = $this->getInvokeArg('bootstrap')->getResource('db');
        $runGateway = new Model_RunGateway(array('dbAdapter' => $dbAdapter));
        $runs = array();
        $runs = $runGateway->getRuns();

        $this->view->runs = $runs; // Will be lost if another action calls $this->render('index')
    }

The form's action is contactprocessAction() ('/index/contactprocess/'). The form is submitted and is not valid. I can assign the $form to a view variable and render the original 'index' view.

    public function contactprocessAction()
    {
        $this->view->valid = false;
        $request = $this->getRequest();
        $form = $this->_getContactForm();
      
        if (!$form->isValid($request->getPost())) {
            $this->view->contactForm = $form; // $this->contactForm is in the view, but no other vars are.
            return $this->render('index');
        }
    }

But, the other view variables that would normally be created when indexAction is called, do not get created.

What do you think is the best way to deal with this?