【问题标题】:Trying to make a contact form example with symfony2尝试使用 symfony2 制作联系表格示例
【发布时间】:2014-07-30 20:45:49
【问题描述】:

我正在尝试填写联系表格然后发送。但是,当我填写表格并单击发送时,我有这个例外:

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的内容:

命名空间教程\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);
}
}

你能给我任何帮助吗!!

【问题讨论】:

  • 你试过bind吗?
  • 谢谢它现在可以使用了!!
  • 太好了,我会添加它作为答案。

标签: php forms symfony


【解决方案1】:

你应该替换

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

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

bindRequest() 已弃用。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        你应该替换

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

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

        bindRequest() 已被弃用。

        【讨论】:

        • 从上面一行复制的答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-26
        • 2016-05-02
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 2019-07-14
        相关资源
        最近更新 更多