簡體   English   中英

Symfony 3 SonataAdmin在表單編輯中以只讀方式顯示“ Sonata_type_collection”字段

[英]Symfony 3 SonataAdmin show a “Sonata_type_collection” field a readonly in Form edit

我有一個“ Sonata_type_collection”字段,只有所有者可以編輯,我希望管理員只能讀取該屬性(他可以編輯其他屬性)。 除此以外,我什么也找不到:

$formMapper->add('commandeElements', 'sonata_type_collection', array('required'=> true,'by_reference' => false,'attr' => array(
            'readonly' => true,
            'disabled' => true
        )), array(
        'edit' => 'inline',
        'inline' => 'table',
        'sortable' => 'position',
    ));

它以某種方式起作用,無法編輯屬性(提交表單時會顯示錯誤消息),但至少可以在視圖中編輯按鈕“添加”和復選框“刪除”以及下拉菜單。

有沒有辦法做到這一點 ?

您可以在options數組中使用btn_add = false來隱藏按鈕

https://sonata-project.org/bundles/admin/3-x/doc/reference/form_types.html#sonata-type-collection

但我可能會嘗試使用細枝檢入前端,檢查用戶是否具有特定角色{% if is_granted('ROLE_ADMIN') %} ... {% endif %}並啟用或禁用表單組件。

我可能會...

{% set disabled = !is_granted('ROLE_YOU_WANT_TO_ALLOW') %} // in your case ROLE_OWNER

然后在渲染時嘗試類似...

{{ form_row(yourForm.yourCollectionName, {
                    'disabled': disabled
                }) }}

以樹枝模板參考為例

http://symfony.com/doc/current/reference/forms/twig_reference.html#form-variables-reference

這個想法可能會讓你做自己想做的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM