How to get form values in Symfony2 controller

前端 未结 13 1489
挽巷
挽巷 2020-12-12 19:17

I am using a login form on Symfony2 with the following controller code

public function loginAction(Request $request)
{
    $user = new SiteUser();
    $form          


        
相关标签:
13条回答
  • 2020-12-12 19:50

    Simply :

    $data = $form->getData();
    
    0 讨论(0)
  • 2020-12-12 19:51

    To get the data of a specific field,

    $form->get('fieldName')->getData();
    

    Or for all the form data

    $form->getData();
    

    Link to docs: https://symfony.com/doc/2.7/forms.html

    0 讨论(0)
  • 2020-12-12 19:52

    If you're using Symfony 2 security management, you don't need to get posted values, you only need to manage form template (see documentation).

    If you aren't using Symfony 2 security management, I advise you strongly to use it. If you don't want to or if you can't, can you give us the LoginType's sources ?

    0 讨论(0)
  • 2020-12-12 19:53

    If you have extra fields in the form that not defined in Entity , $form->getData() doesn't work , one way could be this :

    $request->get("form")["foo"] 
    

    Or :

    $form->get('foo')->getData();
    
    0 讨论(0)
  • 2020-12-12 19:53

    For not mapped form fields I use $form->get('inputName')->getViewData();

    0 讨论(0)
  • 2020-12-12 19:56

    None of the above worked for me. This works for me:

    $username = $form["username"]->getData();
    $password = $form["password"]->getData();
    

    I hope it helps.

    0 讨论(0)
提交回复
热议问题