Stevan Tosic
Stevan Tosic

Reputation: 7199

Generate data attribute to each checkbox in Symfony type

 ->add('research_subject', EntityType::class, array(
    'mapped' => false,
    'class' => Subject::class,
    'label' => 'Research Subject',
    'expanded' => true,
    'multiple' => true,
    'query_builder' => function (EntityRepository $er) {
        $db = $er->createQueryBuilder('w');
        $db ->where($db->expr()->andX(
            $db->expr()->isNotNull('w.pid')
        ));
        $db->orderBy('w.pid', 'ASC');

        return $db;
    },
    'choice_label' => 'name_system',
))

I need to add to each check box data attribute. Is that is possible?

I need that for extra sort of checkboxes in twig latter. I need to group checkboxes by pid value in separate div section.

Upvotes: 2

Views: 308

Answers (2)

Stevan Tosic
Stevan Tosic

Reputation: 7199

'choice_attr' => function($val, $key, $index) {
    return ['data-pid' => $val->getPid()];
},

I had use this as solution, like https://stackoverflow.com/users/4224384/yceruto Suggest.

Upvotes: 1

Ian
Ian

Reputation: 323

You would use the "choice_attr" option for this:

[...]
        'expanded' => true,
        'multiple' => true,
        'choice_attr' => function($val, $key, $index) {

            return ['data' => $key];
        },
[...]

Upvotes: 0

Related Questions