'Symfony form with ChoiceType conditional

I want to create a form in symfony with a double checkbox, in mode You approve the privacy policy or you don't approve the privacy policy.

[]I approve the privacy policy [] I do not approve the privacy policy

The doubt that arises is that one of them will be marked dynamically only if the answer to a query is a value a or another (For example a boolean) (So I think it should be relational), in plan: 1 to approve and 0 to not approve, but previously made the query to generate the form.

What I currently have is the following:

$form = $this->createFormBuilder()
            ->add('LOPD', ChoiceType::class, [
                'multiple' => false,
                'expanded' => true,
                'choices' => [
                    "Approve the privacy policy" => "0",
                    "Do not approve privacy policy" => "1",
                ],
                'data' => '1'
            ])

Then, I don't know how to configure so that the default value, when loading the form, is taken from the User entity. In other words, if the query to user returns a 1, the default option is 0, and the other way around with 0.

I have searched that you can make a query with query_builder, but it is not clear to me how to put the query to do that:

->add('client', EntityType::class, [
                'class' => 'App\Entity\User',
                'expanded' => true,
                'multiple' => false,
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('u')
                        //->where('u.First_Name', ':uid')
                        //->setParameter('uid', $this->getUser()->getId())
                        ->orderBy('u.id', 'DESC');
                },
                /*'choice_label' => 'First_Name',*/
                'choice_label' => 'tipo',
                'choice_value' => 'id'])

How can I do it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source