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

时间:2014-07-30 20:45:50

标签: 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.

这是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);
}
}

你能不能给我任何帮助!!

4 个答案:

答案 0 :(得分:14)

你应该替换

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

使用

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

由于bindRequest()已弃用。

答案 1 :(得分:5)

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

答案 2 :(得分:2)

bindRequest已弃用并已删除,请改用submit方法

答案 3 :(得分:-3)

你应该替换

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

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

已弃用bindRequest()