Skip to content

duplicate_preferred_choices => false - won't populate form option value on edit #58561

@buchmayr

Description

@buchmayr

Symfony version(s) affected

6.4

Description

I use an EntityType element in my form which has preferred choices given. because we use chosen select plugin we don't want duplicates in our chosen search results.
If duplicate_preferred_choices is set to false, stored option values won't be populated in form, if the value is one of the preferred choices.

How to reproduce

class LocationType extends AbstractType
{
    protected EntityManager $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $preferred = $this->em->getRepository(Location::class)->findBy(['token' => ['AT', 'DE', 'CH', 'FR', 'IT', 'GLO', 'INT']], ['token' => 'ASC']);
        $resolver->setDefaults(
            [
                'class' => Location::class,
                'choice_label' => 'selectLabel',
                'placeholder' => '',
                'label' => 'organigram.node.region.label',
                'preferred_choices' => $preferred,
                'duplicate_preferred_choices' => true, // would like to set it false to have no duplicates in chosen search, but it won't be populated on edit if false is set
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('l')
                        ->orderBy('l.name', 'ASC')
                        ->orderBy('l.token', 'ASC');
                },
                'attr' => [
                    'class' => 'chosen',
                ],
            ]
        );
    }

    public function getParent(): string
    {
        return EntityType::class;
    }
}

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions