EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Question: Embedded forms and field types?

Open alex-barylski opened this issue 3 years ago • 1 comments

First off, love this bundle. It's cool and gets way more interesting with each version :)

Curious, I have two scenarios I can't seem to figure out:

  1. I've set a custom form type for address collection. The address has a country field which I would like to use the EA country, rather than Symfony. How would I do this?
  2. I have another field which I would like to be a Symfony form type. Only I want it embedded, not as a collection? Can figure out how that might be done???

Thanks again!!

alex-barylski avatar Jun 28 '22 23:06 alex-barylski

  1. As far as I know there's no easy way to do this..
  2. If I understand correctly you can use:
class ProductCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Product::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            PriceField::new('price'),
        ];
    }
}
final class PriceField implements FieldInterface
{
    use FieldTrait;

    public static function new(string $propertyName, $label = null): self
    {
        return (new self())
            ->setProperty($propertyName)
            ->setLabel($label)
            ->setFormType(PriceType::class)
            ;
    }
}
class PriceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('amount')
            ->add('currency')
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Price::class,
        ]);
    }
}

I recently did a PR for an easier solution but it is not merged yet (https://github.com/EasyCorp/EasyAdminBundle/pull/5353).

michaelKaefer avatar Aug 07 '22 13:08 michaelKaefer