Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] filter entity choicelist after hydration #14072

Closed
@DavidBadura

Description

@DavidBadura

Is it possible to filter the choicelist after querying the entities?
I know that you can filter it with the query_builder option but in some case you can't filter in this way.
For example if you use "array" as column definition in your entity.

I find only bad way to resolve this problem:

$builder->add(
    'contact',
    'entity',
    array(
        'query_builder' => function (EntityRepository $repository) {
            return $repository->createQueryBuilder('c')
                ->where('c.roles LIKE :role')->setParameter('role', '%ROLE_CONTACT%');
        },
        'label'         => 'Contact',
        'class'         => 'AppBundle\Entity\User',
        'required'      => false
    )
)

Or you must implement your own loader.

I think it would be nice and easy to use if you have an option to filter the data after hydration.
Like this:

$builder->add(
    'contact',
    'entity',
    array(
        'choice_filter' => function ($user) {
            return $user->hasRole('ROLE_CONTACT');
        },
        'label'         => 'Contact',
        'class'         => 'AppBundle\Entity\User',
        'required'      => false
    )
)

As with #14050 choice_filter can be a callback or property path. And i think it should be add in ChoiceType so you can filter every choicelist.

I know you should use query_builder to filter the data in the database but in some case you don't have a choice.

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