Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] filter entity choicelist after hydration #14072

Copy link
Copy link
Closed
@DavidBadura

Description

@DavidBadura
Issue body actions

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.