Search code examples
phpsymfony1symfony-formssymfony-1.4admin-generator

Symfony submit to same url


I have a form with some text fields and I have a preview button that needs to submit the form to the same controller. And then in the controller, I need to extract the values and populate a form with these values for the template to see. What is the best way to achieve this? I'm a newbe so please be clear.


Solution

  • Sample controller:

    public function myControllerName(sfWebRequest $request)
    {
      $this->form = new myFormClass();
    }
    

    Use <?php echo $form->renderFormTag( url_for('@yourRoutingName'), array('method' => 'POST') ); ?> in your template and change @yourRoutingName to the one pointing to your controller.

    Now change your controller to be something like this:

    public function myControllerName(sfWebRequest $request)
    {
      $this->form = new myFormClass();
    
      if ($request->isMethod(sfRequest::POST)
      {
        $this->form->bind( $request->getParameter( $this->form->getName() ) );
    
        // Check if the form is valid.
        if ($this->form->isValid())
        {
          $this->form->save();
          // More logic here.
        }
      }
    }
    

    The $this->form->bind( $request->getParameter( $this->form->getName() ) ); part binds posted data to your form where $this->form->isValid() returns a boolean whether the form is valid or not.