如何条件禁用EasyAdmin 4和Symfony 5.4中的字段
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以定义
$ fields
数组,如果语句进行。
如果是正确的,请将字段推到
$ fields
数组,然后最终将其返回。You can define an
$fields
array, make anif
statement.If its true, then push the field to the
$fields
array and finally return it.我面对同样的问题,并通过重建活动侦听器内部的表单来将其修复,以使字段重新添加到所有EasyAdmin所需的选项中。
但是,在您的情况下,棘手的事情是更改/重新创建重建字段,以更改 disabled 值。我想添加/删除字段要比启用/uible更容易。
这是在没有更改过程的情况下获得和重新添加领域的逻辑。
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.