Roland
Roland

Reputation: 303

set selected values for entitytype

I've have a formtype that has an entitytype field.

 $builder->add('canRead', EntityType::class, [
    'attr'          => ['style' => 'height:150px;'],
    'choice_value'  => 'id',
    'required'      => true,
    'multiple'      => true,
    'expanded'      => false,
    'class'         => 'AppBundle\Entity\User',
    'query_builder' => function (EntityRepository $er) {
      $qb = $er->createQueryBuilder('e');
      $qb->innerJoin('e.roles', 'r')->where('r.role LIKE :role')->setParameter('role', 'ROLE_ADMIN_%')
         ->orderBy('e.lastName', 'ASC')
         ->addOrderBy('e.firstGame', 'ASC');

      return $qb;
    },
]);

In the controller, I call this like that:

$form = $this->createForm(MedicalType::class, null, [ 'data'=>[] ]);

Where the 'data' is meant to be the selected values for the generated multiple select-field.

I've tried to pass a single User object, an array of User id's, arraycollection of Users, etc.. All of my tries before was the part from "query_builder"s results of course.

Did not work neither of them. Does someone know a solution to pass "selected" values for an entitytype field in formbuilder?

Thank you in advance.

Upvotes: 2

Views: 6265

Answers (2)

younes elmorabit
younes elmorabit

Reputation: 393

I tried the above solution but it didn't work for me so this might be helpful for someone else.

I had to pass an array of modules to the form so that it will be the default selected values, so in my controller i did this:

function testAction(){
    $modules=array();
    //here i'm just loading the modules that will be the default selected values
    for ($i=7;$i<20;$i++){
        $modules[]=$this->getDoctrine()->getRepository("AppBundle:Module")->find($i);
    }
    //passing the modules to the form
    $form=$this->createForm(ModuleFieldType::class, array(), [ 'data'=> $modules ]);
    return $this->render("/Security/test.html.twig",array("form"=>$form->createView()));
}

And in my form I did this :

$builder->add('module', EntityType::class, array(
                'class' => 'AppBundle\Entity\Module',
                'choice_label' => 'libelle',
                'label' => 'Modules Enseignés',
                'placeholder'=>"selectionez les modules enseigné",
                'group_by' => function (Module $module) {
                    return $module->getDepartement()->getName();
                },
                "attr"=>array("id"=>"modulesSelect"),
                'multiple' => true,
                //you need add this to the form in order to read the data
                'data'=>$options['data'],
            ));

The result was like that in the rendered form:

The rendered form

Upvotes: 0

Cvetan Mihaylov
Cvetan Mihaylov

Reputation: 950

The second parameter to createForm() being null seems to be the problem.

Lets say you have an array of user objects:

$data = [$user1, $user2];

Then try creating the form like that:

$form = $this->createForm(MedicalType::class, $data);

or this way:

$form = $this->createForm(MedicalType::class, array(), [ 'data'=> $data ]);

Upvotes: 2

Related Questions