php - Symfony checkboxes lists twig rendering

547

I'm quite new to Symfony and I have a specific question. I have a form for a job application where the applicant can choose competencies linked to categories.

Entities:

Application linked to Competence as many to many

Compentence linked to Category as Many to One

So in ApplicationType I have:

->add('competences', EntityType::class, array(
    'class'        => 'SanOffresBundle:Competence',
    'label' => 'Compétences (sélectionnez-en autant que vous voulez)',
    'choice_label' => 'nom',
    'multiple'     => true,
    'expanded' => true,
    'query_builder' => function (CompetenceRepository $er) {
        return $er->createQueryBuilder('cc')
        ->orderBy('cc.nom', 'ASC');},
    'group_by' => function($val, $key, $index) {
        return $val->getCategorie()->getNom();},        
    ))

With this code and variations I get:

  • 'expanded' => false : a menu with competences sorted by category, but the categories are not in alphabetical order. This would be an acceptable alternative if the categories were sorted, but with more than 100 competences to choose from, it's not user friendly.

  • 'expanded' => true : a list of competences checkboxes displayed grouped by categories, but the categories are not shown.

Would anybody have an idea?

190

Answer

Solution:

If you'd like to change order in secect with allCompetence than you probably should extend your query builder with join

'query_builder' => function (CompetenceRepository $er) {
        return $er->createQueryBuilder('cc')
               ->join('cc.category', 'cat') //something like that
               ->orderBy('cat.name', 'ASC')->addOrderBy('cc.nom', 'ASC');
}

another way would be to try@ORM\OrderBy({...}) Annotation in your relations betweenCompentence andCategory. See link

'expanded' => true : a list of competences checkboxes displayed grouped by categories, but the categories are not shown.

Which is correct since it's inherited option fromChoiceType. Group by works only if you render it as a<SELECT> dropdown where you can make use of<optgroup> for grouping. I think there's no way to get a bunch (list) of checkboxes with any grouping from symfony's FormTypes out of the box..at least without any extra offer.

Of cource you can create your own FormType and extend Twig-Widgets an so on. ...Or you could try to get desired look with Collection of (embedet) Forms ...but that are just thoughts since I do not know how your entities and their relations look like.

People are also looking for solutions to the problem: php - Get the image path in ajax and delete that image in laravel 5.4

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.