I am using Symfony 5.4 and EasyAdmin 4. I have an entity Suscription with a status property. In the edit form of the corresponding CRUD controller, I want to enable/disable some fields depending on status value. If status is validated I want to disable some fields. If status is not validated I want those fields to be enabled.
In the CRUD controller I have the following :
public function configureFields(string $pageName): iterable
{
return [
...
TextField::new('comments')->setDisabled(true),
...
];
}
I tried adding an eventlistener like this, it's working but the field is moved to the end of the form:
public function createEditFormBuilder(EntityDto $entityDto, KeyValueStore $formOptions, AdminContext $context): FormBuilderInterface
{
$builder = $this->container->get(FormFactory::class)->createEditFormBuilder($entityDto, $formOptions, $context);
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$entity = $event->getData();
$form = $event->getForm();
if($entity->getStatus() != 'validated') {
$form->add('comments', TextType::class);
}
});
return $builder;
}
How can I do to conditionally enable/disable the field without moving it to the end of the form when rendering it ?
Thanks in advance !