尝试使用symfony2建立联系表单示例 [英] Trying to make a contact form example with symfony2
本文介绍了尝试使用symfony2建立联系表单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里尝试填写联系表格,然后发送.但是,当我填写表格并单击发送"时,我会遇到以下异常:
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.
这是ContactHandler.php的内容:
This is the content of ContactHandler.php:
命名空间tuto \ WelcomeBundle \ Form \ Handler;
namespace tuto\WelcomeBundle\Form\Handler;
使用Symfony \ Component \ Form \ Form; 使用Symfony \ Component \ HttpFoundation \ Request;
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('xxxx@gmail.com')
->setBody($data['content']);
$this->mailer->send($message);
}
}
请给我任何帮助!
推荐答案
您应该替换
$this->form->bindRequest($this->request);
使用
$this->form->bind($this->request);
bindRequest()
已弃用.
这篇关于尝试使用symfony2建立联系表单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文