Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to make a contact form example with symfony2

Tags:

forms

php

symfony

Here I'm trying to fill in a contact form then send it. However when I fill in the form and click on send I have this exception :

UndefinedMethodException: Attempted to call method "bindRequest" on class "Symfony\Component\Form\Form" in /symfony/src/tuto/WelcomeBundle/Form/Handler/ContactHandler.php line 47.

This is the content of ContactHandler.php:

namespace tuto\WelcomeBundle\Form\Handler;

use Symfony\Component\Form\Form; use Symfony\Component\HttpFoundation\Request;

/**
* The ContactHandler.
* Use for manage your form submitions
*
* @author Abderrahim
*/
class ContactHandler
{
 protected $request;
 protected $form;
 protected $mailer;

 /**
 * Initialize the handler with the form and the request
 *
 * @param Form $form
 * @param Request $request
 * @param $mailer
 * 
 */
 public function __construct(Form $form, Request $request, $mailer)
 {
    $this->form = $form;
    $this->request = $request;
    $this->mailer = $mailer;
 }

 /**
 * Process form
 *
 * @return boolean
 */
  public function process()
 {
  // Check the method
  if ('POST' == $this->request->getMethod())
  {
      // Bind value with form
      $this->form->bindRequest($this->request);

      $data = $this->form->getData();
      $this->onSuccess($data);

      return true;
  }

  return false;
}

/**
 * Send mail on success
 * 
 * @param array $data
 * 
 */
protected function onSuccess($data)
{
    $message = \Swift_Message::newInstance()
                ->setContentType('text/html')
                ->setSubject($data['subject'])
                ->setFrom($data['email'])
                ->setTo('[email protected]')
                ->setBody($data['content']);

    $this->mailer->send($message);
}
}

Could you please give me any help !!

like image 748
Othmane Avatar asked Jul 30 '14 20:07

Othmane


2 Answers

You should replace

$this->form->bindRequest($this->request);

With

$this->form->bind($this->request);

As bindRequest() has deprecated.

like image 137
Alex Avatar answered Oct 26 '22 23:10

Alex


Use $form->handleRequest($request); to handle form submissions - http://symfony.com/doc/current/book/forms.html#handling-form-submissions

like image 26
dmnptr Avatar answered Oct 27 '22 00:10

dmnptr