我正在使用 EasyAdmin 4 (EA4),我想根据同一实体上的值禁用字段。
例如,Reviewer可能与User相关联,在这种情况下,我想禁用其他字段,例如name。
所以我创建了一个Voter,它获取Reviewer并检查是否
$reviewer->getUser() === null
。
但是,我发现甚至不可能使用 setPermission
正确隐藏
name字段,因为 Voter 获得没有值的 FieldDTO。
所以,我迷路了。我怎么会出现这种行为?
这是我迄今为止找到的解决方案(我希望有更好的解决方案):
public function createEditForm(EntityDto $entityDto, KeyValueStore $formOptions, AdminContext $context): FormInterface
{
if ($entityDto->getInstance()->getUser() !== null)
{
$entityDto->getFields()->getByProperty('name')->setFormTypeOption('disabled', true);
$entityDto->getFields()->getByProperty('institution')->setFormTypeOption('disabled', true);
$entityDto->getFields()->getByProperty('email')->setFormTypeOption('disabled', true);
}
return parent::createEditForm($entityDto, $formOptions, $context);
}