Frage

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.

War es hilfreich?

Lösung

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.

Andere Tipps

Have you tried this ?

$this->redirect($request->getReferer()); //action

if not, then please try and check if its work for you.

Thanks.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top