How is the best way to add select with choices to filters in Sonata Admin?

For your admin class you should use configureDatagridFilters function to add your filters,if you want to add custom options for your gender fields you can use doctrine_orm_string and provide your choices list in array form

$datagridMapper
       ->add('gender',
        'doctrine_orm_string',
        array(), 
       'choice',
        array('choices' => array('m' => 'Male', 'f' => 'Female')
        )
    );

On my version - symfony 3.4 and "sonata-project/doctrine-orm-admin-bundle": "^3.0"

worked this way:

->add('preferredLanguage', 'doctrine_orm_choice', [
                    'global_search' => true,
                    'field_type' => ChoiceType::class,
                    'field_options' => [
                        'choices' => [
                            'English' => PotentialCustomerInterface::PREFERRED_LANGUAGE_ENGLISH,
                            'Spanish' => PotentialCustomerInterface::PREFERRED_LANGUAGE_SPANISH
                        ]
                    ]
                ]
            )

The choices are string values in database.

If you want choices from database filtered by some logic:

    ->add('csr', 'doctrine_orm_choice', [
            'field_type' => EntityType::class,

            'field_options' => [
                'class' => User::class,
                'query_builder' => function (UserRepository $userRepository) {
                    return $userRepository->qbFindAdmins();
                },
            ]

        ]
    )

In UserRepository just create method which returns query builder.


Try this:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{

    $datagridMapper

       ->add('gender',null,  array(), ChoiceType::class, array(
           'choices' => array('m' => 'Male', 'f' => 'Female')
       ))
    ;
}