Search code examples
sonata-adminsonata

Sonata admin datagrid filter can't get ChoiceType to work


I'm removing my hairs over this one. On sonata admin 3.x I had this filter in the list view, providing a select box with the options described.


protected function configureDatagridFilters(DatagridMapper $datagridMapper): void 
{
    ->add('state', 'doctrine_orm_choice',
                array('label' => 'State'),
                ChoiceType::class, array(
                    'choices' => array(
                        'new' => 'new',
                        'open' => 'open',
                        'closed' => 'closed' ),
                        'required' => false

                    )
            )
}

But on the upgrade to 4.x I got the following error :

No attached service to type named 'doctrine_orm_choice'

I tried everything between the ChoiceType, to the ChoiceFilter but I can't find any snippet on the docs or any relevant cue on how this is supposed to work now.

Thanks a lot !


Solution

  • And the correct syntax is :

    use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter;
    
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void 
    {
         ->add('state',   ChoiceFilter::class, ['label' => 'State',
                        'field_type' => ChoiceType::class,
                        'field_options' => [
                            'choices' => [
                                'new' => 'new',
                                'open' => 'open',
                                'closed' => 'closed'],
                            'required' => false
    
                        ]
                    ]
                )
    }