Home > other >  Custom nested form type easyadmin
Custom nested form type easyadmin

Time:12-13

I have an entity User and an entity Address which are in OneToOne relationship. I would like to display the address type in the User Crud in EasyAdmin, and I don't find a way to do just like Symfony ->add('address', AddressType::class). I tried the following options:

CollectionField::new('address')
            ->setEntryIsComplex(true)
            ->setEntryType(AddressType::class)
            ->setFormTypeOptions([
                'by_reference' => false,
                'required' => true
            ]),

But this makes the user able to add as many as addresses he wants, although I just want one.

AssociationField::new('address')->hideOnIndex()

This one makes the user choose an existing address in a list. That's not an embed of a form type.

Does anyone have an idea?

CodePudding user response:

The solution I found is as follows:

Create Address Field like this

<?php

namespace App\Field;

use App\Form\AddressType;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;

final class AddressField implements FieldInterface
{
    use FieldTrait;

    public static function new(string $propertyName, ?string $label = 'Address'): self
    {
        return (new self())
            ->setProperty($propertyName)
            ->setLabel($label)
            // this template is used in 'index' and 'detail' pages
            ->setTemplatePath('admin/field/address.html.twig')
            // this is used in 'edit' and 'new' pages to edit the field contents
            // you can use your own form types too
            ->setFormType(AddressType::class)
            ->addCssClass('field-address')
        ;
    }
}

And then use it in your User Crud Controller

public function configureFields(string $pageName): iterable
{
  // Other fields

  yield AddressField::new('address'); // Your new address field
}

templates/admin/field/address.html.twig template would be like this

{% if field.value is defined %}  
  <dl >
    <dt >City</dt>
    <dd >{{ field.value.city }}</dd>
    <dt >Address 1</dt>
    <dd >{{ field.value.address1 }}</dd>
    <dt >Address 2</dt>
    dd >{{ field.value.address2 }}</dd>
  </dl>
{% endif %}
  • Related