处理表单事件

为了能够处理表单事件,将请求(在提交表单后发送到控制器操作)附加到在该操作中创建的表单非常重要。

public function registerAction(Request $request)
{
    $data = new ExampleObject();
    $form = $this->createForm(ExampleObjectType::class, $data);

    $form->handleRequest($request);
    if($form->isSubmitted() && $form->isValid()){
        // do something with form data
        return $this->redirectToRoute('route_name');
    }
    return $this->render('view.html.twig', array(
        'form' => $form->createView()
    ));
}

传递给操作的请求变量的类型为 Symfony\Component\HttpFoundation\Request