Search code examples
phpformssymfony1

How to retrieve a form from processFrom into an other action?


I want to retrieve data from my first form into my function executeAddDomaines to use these into a second form called by this function but when i give $form in parameters to this function i have this error :

Catchable fatal error: Argument 2 passed to domaineActions::executeAddDomaines() must be an instance of sfForm, none given, called in /home/webs/extranet100p100.net/htdocs/lib/vendor/symfony/lib/action/sfActions.class.php on line 60 and defined in /home/webs/extranet100p100.net/htdocs/apps/backend/modules/domaine/actions/actions.class.php on line 206

This is my code:

protected function processForm(sfWebRequest $request, sfForm $form)
{
$name = $form->getName();

$form->bind($request->getParameter($name), $request->getFiles($name));

if($form->isValid())
{

    if($form->isNew())
        {   
        $this->executeAddDomaines($request, $form);
        $this->redirect('@add_domaines');

         }
[...]
}

public function executeAddDomaines(sfWebRequest $request, sfForm $form)
{       

      $name = $form->getName();

      $params = $request->getParameter($name);

      $this->list_domaines = $params;


      $this->form = new AddDomainesForm();


      $this->setTemplate('listDomaines');
}

So how i can do it ?

Thank you :)


Solution

  • You can try to save the form data in session and then get it in the 2nd action.

    Try this (i didn't test):

    protected function processForm(sfWebRequest $request, sfForm $form)
        {
        $name = $form->getName();
    
        $form->bind($request->getParameter($name), $request->getFiles($name));
    
        if($form->isValid())
        {
    
            if($form->isNew())
                {   
                # Replaced
                $request->setAttribute("form", $form);
                $this->redirect('@add_domaines');
    
                 }
        [...]
        }
    
        public function executeAddDomaines(sfWebRequest $request)
        {      
              # Added
              $form = $request->getAttribute("form");
    
              $name = $form->getName();
    
              $params = $request->getParameter($name);
    
              $this->list_domaines = $params;
    
    
              $this->form = new AddDomainesForm();
    
    
              $this->setTemplate('listDomaines');
        }
    

    After use it, you can clean the attribute form from the request