尝试用symfony2制作一个联系人表单示例


Trying to make a contact form example with symfony2

在这里,我试图填写一份联系表格,然后发送它。然而,当我填写表格并点击发送时,我有一个例外:

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

这是ContactHandler.php:的内容

命名空间tuto''WelcomeBundle''Form''Handler;

使用Symfony''Component''Form''Form;使用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('xxxx@gmail.com')
                ->setBody($data['content']);
    $this->mailer->send($message);
}
}

你能帮我吗!!

您应该更换

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

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

由于bindRequest()已弃用。

使用$form->handleRequest($request);处理表单提交-http://symfony.com/doc/current/book/forms.html#handling-表单提交

bindRequest已弃用并已删除,请使用submit方法代替

您应该更换

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

带有

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

因为CCD_ 5已经被弃用。