-1

I am trying to create a form using Symfony 2.5 using this tutorial, but this tutorial is using old version of Symfony. I can get the form to display and created the entity as well however I am now working on submitting the form. Following is the code from tutorial and this code is inside default controller contactAction

public function contactAction()
{
    $enquiry = new Enquiry();
    $form = $this->createForm(new EnquiryType(), $enquiry);

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // Perform some action, such as sending an email

            // Redirect - This is important to prevent users re-posting
            // the form if they refresh the page
            return $this->redirect($this->generateUrl('BloggerBlogBundle_contact'));
        }
    }

    return $this->render('BloggerBlogBundle:Page:contact.html.twig', array(
        'form' => $form->createView()
    ));
}

My main concerns is in the following section of above code

$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
    $form->bindRequest($request);

As you will notice it is using getRequest() which has been depricated and then my IDE is telling me buildRequest method cannot be found.

I will really appreciate if someone call push me towards the right path of converting the contactAction for symfony verion 2.5, I will really appreciate it.

4

4 回答 4

2

像这样声明动作:

public function contactAction(Request $request)
{
...
}

并导入:

use Symfony\Component\HttpFoundation\Request;

您将在您的操作中包含请求,因此您可以删除此行:

$request = $this->getRequest();
于 2014-09-22T12:34:44.927 回答
1

嗨,有一些不推荐使用的电话,我真的建议你去 Symfony 的食谱。但无论如何,下面的内容会有所帮助。

namespace myproject/mybundle/controller;

use Symfony\Component\HttpFoundation\Request;

Class Act //that is me ;) {

    /**
     * @Route("/contact", name="_lalalalala")
     * @Template()
    */
    public function contactAction(Request $request){
    $enquiry = new Enquiry();
    $form = $this->createForm(new EnquiryType(), $enquiry);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($enquiry);
        $em->flush();

        return $this->redirect($this->generateUrl('BloggerBlogBundle_contact'));
    }

    return ['form' => $form->createView()];
  }
}

您可以通过使用 symfony 服务容器注入您的表单来进一步缩短此代码。我建议阅读这篇文章,它非常棒。因为您可以在任何地方重复使用表单:)

于 2014-09-22T12:45:08.553 回答
0

您可以更改getMethodisMethod喜欢

if ($request->isMethod('POST'))

submit然后您可以使用like将请求数据提交到表单

$form->submit($request->request->get($form->getName()));

或者,您可以使用handleRequest一次性处理上述两种方法的方法,然后您可以继续执行其余操作,例如

$form->handleRequest($request);

if ($form->isValid())
    .. etc
于 2014-09-22T12:45:50.170 回答
0

要检索请求对象,您有两种可能性。要么让Symfony 将请求作为参数传递给您的控制器操作......

public function contactAction(Request $request)
{
    // ...
}

...或从容器中获取请求对象。

$request = $this->get('request');

对于将请求绑定到表单,手册说明如下:

将请求直接传递给 submit() 仍然有效,但已被弃用,并将在 Symfony 3.0 中删除。您应该改用方法handleRequest()

这样,请求绑定部分将如下所示:

if ($request->isMethod('POST')) {
    $form->handleRequest($request);

    if ($form->isValid()) {
        // do something
    }
}
于 2014-09-22T12:46:45.810 回答