How to get form values in Symfony2 controller

前端 未结 13 1500
挽巷
挽巷 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 20:07

    In Symfony >= 2.3, you can get the value of single fields with:

    $var = $form->get('yourformfieldname')->getData();
    

    On the other hand, you can use:

    $data = $form->getData();
    

    BUT this would get you two different things:

    • the entity with values populated by the form, if your form have the data-class option enabled (so it's binded to an entity); this will exclude any field with the 'mapping' => false option

    • otherwise, an array with all the form's fields

提交回复
热议问题