简体   繁体   中英

How get form in controller action Symfony 2

I trying to send Ajax request to my Controller, get form there, and if some data true, need to remove some part of form, but I dont know how i can get my form trought $request object. Help me please.

All code for now, I tell there is exist some way, to do something like this $request->getForm()

public funcion ajaxAction(Request $request)
{
     if ($request->isXmlHttpRequest()) {

      }
}

You should do something like this (I assume you're in your controller extending Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller ):

public funcion ajaxAction(Request $request)
{
    if ($request->isXmlHttpRequest()) {
        $form = $this->createForm(new YourFormType());
        $form->handleRequest($request);
    }
}

after that you have $form with data from requst bound. You can call methods like isValid() or getData() on this variable

Check documentation for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM