How to get form values in Symfony2 controller

前端 未结 13 1491
挽巷
挽巷 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:59

    I think that in order to get the request data, bound and validated by the form object, you must use this command :

    $form->getViewData();
    $form->getClientData(); // Deprecated since version 2.1, to be removed in 2.3.
    
    0 讨论(0)
  • 2020-12-12 20:00

    I got it working by this:

    if ($request->getMethod() == 'POST') {
        $username = $request->request->get('username');
        $password = $request->request->get('password');
    
        // Do something with the post data
    }
    

    You need to have the Request $request as a parameter in the function too! Hope this helps.

    0 讨论(0)
  • 2020-12-12 20:04
    private function getFormDataArray($form)
    {
        $data = [];
        foreach ( $form as $key => $value) {
            $data[$key] = $value->getData();
        }
        return $data;
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-12 20:10

    In Symfony 2 ( to be more specific, the 2.3 version ) you can get a data of an field by

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

    or you can get all data sent

    $data = $form->getData();
    

    where '$data' is an array containing your form fields' values.

    0 讨论(0)
  • 2020-12-12 20:17

    In Symfony forms, there are two different types of transformers and three different types of underlying data: In any form, the three different types of data are:

    • Model data

      This is the data in the format used in your application (e.g. an Issue object). If you call Form::getData() or Form::setData(), you're dealing with the "model" data.

    • Norm Data

      This is a normalized version of your data and is commonly the same as your "model" data (though not in our example). It's not commonly used directly.

    • View Data

      This is the format that's used to fill in the form fields themselves. It's also the format in which the user will submit the data. When you call Form::submit($data), the $data is in the "view" data format.

    The two different types of transformers help convert to and from each of these types of data:

    • Model transformers:

      transform(): "model data" => "norm data"
      reverseTransform(): "norm data" => "model data"

    • View transformers:

      transform(): "norm data" => "view data"
      reverseTransform(): "view data" => "norm data"

    Which transformer you need depends on your situation.

    To use the view transformer, call addViewTransformer().


    If you want to get all form data:

    $form->getData();
    

    If you are after a specific form field (for example first_name):

    $form->get('first_name')->getData();
    
    0 讨论(0)
提交回复
热议问题