Search code examples
sonata-adminsymfony6

How can I configure Sonata Admin form to handle Enum types in a 'json' column for a User table?


  1. User table with 'roles' column with type json (roles - longtext - not null - comment '(DC2Type:json)');

  2. Trying to save User via Sonata Admin tool and get the error Expected argument of type "array", "App\\UserModule\\Entity\\Enum\\UserRole" given at property path "roles".

    protected function configureFormFields(FormMapper $form): void
    {
        $form->add('roles', EnumType::class, ['class' => UserRole::class]);
    }
    

Image

Form field is correctly displayed, but handling post request - got error. Creating user via console command works nice, so this is only about Sonata stuff


Solution

  • One of the way it can be done - use ChoiceType with 'multiple' setting

            $form->add('roles', ChoiceType::class, [
            'choices' => $roles,
            'multiple' => true
        ]);