2

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 !

LuD_GRi
  • 63
  • 7

2 Answers2

0

You can define an $fields array, make an if statement.

If its true, then push the field to the $fields array and finally return it.

public function configureFields(string $pageName): iterable
{
    $fieldsArray = [];
    if (YOUR_CONDITION_HERE) {
       $fieldsArray[]  = TextField::new('comments')->setDisabled(true);
    }
    return $fieldsArray;
}
Dhia Djobbi
  • 1,176
  • 2
  • 15
  • 35
  • 1
    Thank yo @Dhia Djobbi . The condition depends on the entity. At this stage we do not have the entity, or may I am wrong? – LuD_GRi Jun 24 '22 at 12:49
0

I faced to the same kind of issue and fixed it by rebuilding the form inside the event listener to get the field to re-add with all EasyAdmin needed options.

However, the tricky thing in your case would be to alter/re-create the rebuilt field in order to change the disabled value. I guess it would be easier to add/remove the field than to enable/diable it.

Here is the logic to get and re-add your field without the alteration process.

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') {
            // Start form Here…
            // Rebuild the form.
            $newForm = $this->createEditFormBuilder($context->getEntity(), $context->getCrud()->getEditFormOptions(), $context)->getForm();
            // Get the field to alter from the rebuilt form.
            $comments = $newForm->get('comments'); 
            
            // The tricky thing here would be to alter/re-create the getted field.
   
            // Re-add the altered field.
            $form->add($comments);
            // Here your are!
        }
    });
    return $builder;
}
NicolasGraph
  • 71
  • 1
  • 4