尝试使用symfony2创建联系表单示例

Oth*_*ane 5 php forms symfony

在这里,我正在尝试填写联系表格然后发送.但是,当我填写表单并单击发送时,我有以下异常:

UndefinedMethodException: Attempted to call method "bindRequest" on class "Symfony\Component\Form\Form" in /symfony/src/tuto/WelcomeBundle/Form/Handler/ContactHandler.php line 47.
Run Code Online (Sandbox Code Playgroud)

这是ContactHandler.php的内容:

namespace 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);
}
}
Run Code Online (Sandbox Code Playgroud)

能不能给我任何帮助!!

Ale*_*lex 14

你应该更换

$this->form->bindRequest($this->request);
Run Code Online (Sandbox Code Playgroud)

$this->form->bind($this->request);
Run Code Online (Sandbox Code Playgroud)

由于bindRequest()已经过时.


dmn*_*ptr 5

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