Link to home
Start Free TrialLog in
Avatar of DTDI
DTDI

asked on

Symfony 4 form with one-to-many relationship as checkbox

In symfony 4 i have a form to manage user details.

There is a one-to-many relationship between User class and SecurityGroup class.

In my form i would like to have all SecurityGroup listed with checkboxes.
I've managed to do it, but if i check a checkbox the group is not added to the user after the handle request. (In the request dump i see my 2 checked groups)

Form Field:
->add('SecurityGroups', EntityType::class, [
                'class'     => 'App\Entity\SecurityGroup',
                'choice_label' => 'displayname',
                'query_builder' => function (SecurityGroupRepository $repo) {
                    return $repo->createQueryBuilder('f')
                        ->where('f.id > :id')
                        ->setParameter('id', 1);
                },
                'label'     => 'Member Of?',
                'expanded'  => true,
                'multiple'  => true,
            ])

Open in new window


Controller
public function edit(Request $request, $id)
    {

        $user = new User();

        if ($id == -1) {
            $module_title = 'Create a new User';
        }
        else
        {
            $module_title = 'Edit User';
            $user = $this->getDoctrine()->getRepository("App:User")->find($id);
        }

        $form = $this->createForm("App\Form\UserType", $user);


        $form->handleRequest($request);

        $current_user = $this->getUser();
        dump($request);
        dump($form);

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

            $user = $form->getData();
            dump($user);
            
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();
            $this->redirectToRoute('admin_user');
        }



        return $this->render('user/edit.html.twig', ['edit_form' => $form->createView(), 'module_title' => $module_title]);
    }

Open in new window


Dumps
Request {#86 ▼
  +attributes: ParameterBag {#70 ▶}
  +request: ParameterBag {#69 ▼
    #parameters: array:1 [▼
      "user" => array:8 [▼
        "username" => "admin"
        "email" => "email@email.email"
        "password" => ""
        "isActive" => "1"
        "DisplayName" => "admin"
        "SecurityGroups" => array:2 [▼
          0 => "2"
          1 => "4"
        ]
        "save" => ""
        "_token" => "YaqXjd73ZhrX8p1eOiFWZi1JWivaCLKMGZbulTsiRgE"
      ]
    ]
  }

Open in new window

Avatar of Kyle Santos
Kyle Santos
Flag of United States of America image

Hi,

I am following up on your question.  Do you still need help?

If you solved the problem on your own, would you please post the solution here in case others have the same problem?

Regards,

Kyle Santos
Customer Relations
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.