You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sulu CMS如何基于单选关联资源的底层属性设置字段可见性条件

解决方案

你遇到的核心问题是默认的single_selection字段仅会将选中项的ID存入表单数据,visibleCondition自然无法拿到allowData属性,按以下步骤调整即可实现需求:

1. 调整字段配置返回完整对象

修改sulu_admin.yaml中single_gatekeeper_selection的配置,增加return_type: object参数,让字段选中后将完整的Gatekeeper对象存入表单数据,而非仅存ID:

sulu_admin:
  field_type_options:
    selection:
      # 原有data_selection配置保持不变
      data_selection:
        default_type: 'list_overlay'
        resource_key: 'data'
        types:
          list_overlay:
            adapter: 'table'
            list_key: 'data'
            display_properties: ['value']
            icon: 'su-plus'
            label: 'app.data'
            overlay_title: 'app.action.select.data'
    single_selection:
      single_gatekeeper_selection:
        default_type: 'single_select'
        resource_key: 'gatekeepers'
        types:
          single_select:
            display_property: 'name'
            id_property: 'id'
            overlay_title: 'app.action.select.gatekeeper'
            return_type: object # 新增此行配置

2. 调整序列化配置暴露所需字段

确保你的序列化配置(通常为JMS Serializer配置)满足以下要求:

  • Gatekeeper实体的allowData字段被加入管理员接口对应的序列化组
  • Provision实体的keeper关联属性被加入表单详情接口的序列化组,同时配置最大深度避免循环引用

参考序列化配置示例:

App\Entity\Gatekeeper:
    attributes:
        id:
            groups: ['admin_list', 'admin_detail']
        name:
            groups: ['admin_list', 'admin_detail']
        allowData:
            groups: ['admin_list', 'admin_detail']

App\Entity\Provision:
    attributes:
        keeper:
            groups: ['admin_detail']
            max_depth: 1

3. 调整后端数据映射逻辑

修改Provision表单提交的处理逻辑,之前你直接从请求参数里取gatekeeper的ID关联实体,现在需要从返回的对象中取ID:

// 调整前
// $gatekeeper = $gatekeeperRepository->find($request->get('gatekeeper'));

// 调整后
$gatekeeper = $gatekeeperRepository->find($request->get('gatekeeper')['id']);
$provision->setKeeper($gatekeeper);

调整完成后你原本写的visibleCondition="!!gatekeeper && gatekeeper.allowData"就可以正常生效,选择对应Gatekeeper后data字段会自动按allowData的值显示或隐藏。


内容的提问来源于stack exchange,提问作者Leon Willens

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 20:57:03