Home > front end >  Symfony - Error with the data parameter on a form
Symfony - Error with the data parameter on a form

Time:07-25

Context of the problem :

I created a symfony form.

Each tool has a collection of modules.

The user has a collection of modules of any tool.

What I want :

I want for each tool there are checkboxes corresponding to the tool's modules. The module checkboxes that the user owns are checked.

([] = checkbox)

Tool1 : []Module1 [x]Module2 [x]Module3

Tool2 : []Module4 [x]Module5

Tool3 : [x]Module6 []Module7

What I currently have:

For each tool, there are checkboxes corresponding to the tool's modules. But I have a problem to tick the checkboxes of user's modules. I get an error on the data parameter.

The form field :

 $user = $options['user'];
 $tools = $options['tools'];

        foreach ($tools as $tool) {
            $name = 'profile_'.str_replace(array('-', ' ', '.'), '', $tool->getLibelle());
            $builder
                ->add($name, ChoiceType::class, [
                    'label' => $tool->getLibelle(),
                    'choices' => $tool->getModules(),
                    'choice_value' => 'id',
                    'choice_label' => function (?Module $module) {
                        return $module ? $module->getName() : '';
                    },
                    'data'=> $user->getModules(), // ERROR HERE
                    'expanded' => true,
                    'multiple' => true,
                    'mapped'=>false
                ])
            ;
        }

[...]

public function configureOptions(OptionsResolver $resolver): void
{
    $resolver->setDefaults([
        'data_class' => User::class,
        'user'=> null,
        'category'=> null,
        'tools'=> null,
    ]);
}

The error :

ERROR

My question :

Why do I have this error? How can I use the data parameter correctly to achieve the expected result?

CodePudding user response:

You are on the good way, try to dump what is $user->getModules() returning, it has to be an array. May be is not returning an array, check te relation.

I did a little test and it works perfectly.

$name = 'name_field';
$builder->add($name,ChoiceType::class, array(
    'choices' => array('Yes', 'No'),
    'data' => array('Yes', false),
    'mapped' => false,
    'expanded' => true,
    'multiple' => true
));

CodePudding user response:

Here the solution :

Seems to me that $user->getModules() returns a collection. I managed to find another solution and that works (I changed the type of the field to EntityType)

foreach ($tools as $tool) {
            $name = 'acces_'.str_replace(array('-', ' ', '.'), '', $tool->getLibelle());
            $builder
                ->add($name, EntityType::class, [
                    'class'=> Module::class,
                    'label' => $tool->getLibelle(),
                    'data' => $user->getModules(),
                    'choices'=> $tool->getModules(),
                    'choice_value' => 'id',
                    'choice_label' => 'name',
                    'expanded' => true,
                    'multiple' => true,
                    'required' => true,
                    'mapped'=>false,
                ])
            ;
        }

ChoiceType: data parameter need array

EntityType: data parameter need collection

Thanks for the help !

  • Related