Home > OS >  Symfony post data should not be null in request->get()
Symfony post data should not be null in request->get()

Time:09-18

I have extra field on my form. This extra field not use Symfony Form. I want to get values of this extra field in my controller.

Twig code :

{% for module in tool.modules %}
       <input type="checkbox"  id="user_access_{{ tool.id }}_{{ module.id }}" name="user[access_{{ tool.id }}][]" value="{{ module.id }}">                                                                                  
   <label for="user_access_{{ tool.id }}_{{ module.id }}">{{ module.name }}</label>
{% endfor %}

Controller code :

    /**
 * @Route("/{id}/edit", name="user_edit", methods={"GET", "POST"})
 */
public function editUser(Request $request, User $user, ModuleRepository $moduleRepository): Response
{
    //some code

    $form = $this->createForm(UserType::class, $user, array('user'=>$user, 'category'=>$categ, 'tools'=>$tools));

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {


        dd($request); // parameter access_278 contains data I want
        dd($request->request->get('user[access_278][]')); // GET NULL :(

Results of dd($request)

(The values I want is for access_278)

As you can see on the results pic, the request contains the data of access_278.

Why in : dd($request->request->get('user[access_278][]')) I get "null" when I should get the values of the checkboxes?

Maybe I am using a wrong way to retrieve the data?

CodePudding user response:

You are accessing the property in a bad way. Try this instead:

$request->get("user['access_278']");
  • Related