EasyAdminBundle
EasyAdminBundle copied to clipboard
Question: Embedded forms and field types?
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:
- 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?
- 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!!
- As far as I know there's no easy way to do this..
- 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).