EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.02k stars 1.02k forks source link

Force field type on nested property #5205

Open Zer0NimO-web opened 2 years ago

Zer0NimO-web commented 2 years ago

Hello,

I'm having trouble with field type of nested properties. Let's assume we have the following entities : one product for many product options

Capture d’écran 2022-04-27 à 14 32 06

I want to be able to edit ProductOption inside ProductCrudController.

Therefore i did a Form for the options which is like :

  public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, ['label' => 'Nom'])
            ->add('image', TextType::class, ['label' => 'Images'])    //Inside the ProductOption entity image is a string
        ;
    }

And i set the following line inside ProductCrudController:

          CollectionField::new('options')
                ->setEntryType(ProductOptionType::class)
                ->onlyOnForms(),

The issue is image field appears as a TextField inside the form and i want it to be an ImageField. Capture d’écran 2022-04-27 à 14 29 40

Is there a way to force ImageField on nested ProductOption.image property ?

Zer0NimO-web commented 2 years ago

I found a solution for those who are looking for an answer. I replaced this :

 ->add('image', TextType::class, ['label' => 'Images'])  

by this :

            ->add('images', FileUploadType::class, [
                'label' => 'Images',
                'multiple' => true,
                'upload_dir' => 'public/uploads/images/',
            ])

Inside buildForm. It doesn't look perfect but at least it works. Capture d’écran 2022-04-28 à 13 56 46

Here is what the field looks like after clicking on the trash icon : Capture d’écran 2022-04-28 à 14 00 16