如何条件禁用EasyAdmin 4和Symfony 5.4中的字段

发布于 2025-02-09 19:32:31 字数 1131 浏览 2 评论 0原文

我正在使用Symfony 5.4和EasyAdmin 4。 我有一个带有状态属性的实体官方。 在相应的CRUD控制器的编辑形式中,我想根据状态值启用/禁用某些字段。如果状态已验证,我想禁用某些字段。如果未验证状态,我希望启用这些字段。

在CRUD控制器中,我有以下内容:

public function configureFields(string $pageName): iterable
{
    return [        
        ...
        TextField::new('comments')->setDisabled(true),
        ...
    ];

}

我尝试添加这样的EventListener,它在工作,但该字段已移至表单的末尾:

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;
}

我该如何有条件地启用/禁用该字段而不将其移动到末端渲染时形式?

提前致谢 !

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁我热情 2025-02-16 19:32:31

您可以定义$ fields数组,如果语句进行

如果是正确的,请将字段推到$ fields数组,然后最终将其返回。

public function configureFields(string $pageName): iterable
{
    $fieldsArray = [];
    if (YOUR_CONDITION_HERE) {
       $fieldsArray[]  = TextField::new('comments')->setDisabled(true);
    }
    return $fieldsArray;
}

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;
}
生活了然无味 2025-02-16 19:32:31

我面对同样的问题,并通过重建活动侦听器内部的表单来将其修复,以使字段重新添加到所有EasyAdmin所需的选项中。

但是,在您的情况下,棘手的事情是更改/重新创建重建字段,以更改 disabled 值。我想添加/删除字段要比启用/uible更容易。

这是在没有更改过程的情况下获得和重新添加领域的逻辑。

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文