简体   繁体   English

Symfony2 Sonata Admin 仅将属性显示为只读文本

[英]Symfony2 Sonata Admin show attribute only as a readyonly text

I have some immutable attributes on my entity to administrate with sonata-admin bundle.我的实体上有一些不可变的属性可以使用 sonata-admin 包进行管理。

I want to show them in the edit-view of the entity, but don't want to provide any mechanism to change it (eg the value shall not be inside a input field)我想在实体的编辑视图中显示它们,但不想提供任何更改它的机制(例如,值不应在输入字段内)

I couldn't find anything but this:除了这个我找不到任何东西:

$formMapper
    ->add('post', 'entity', 
        array(
            'label' => 'Some post', 
            'attr' => array(
                'readonly' => true,
                'disabled' => true
            ),
            'class' => 'Acme\DemoBundle\Entity\Post'
        )
    )
;

I tried it out with read_only , readonly , disabled etc. all the stuff.我用read_onlyreadonlydisabled等尝试了所有的东西。 It looks ok, it's now inside a dropdown (since it is an entity) and I can not modify it.看起来不错,它现在位于下拉列表中(因为它是一个实体),我无法修改它。

But I even don't want that.但我什至不想那样。 I really need it as text (the current one).我真的需要它作为文本(当前文本)。

Especially this is annoying if you use DoctrineExtensions with softdeletable, timestampable, since every "save" saves also the form-data.如果您使用具有可软删除、可时间戳的 DoctrineExtensions,这尤其令人讨厌,因为每次“保存”也会保存表单数据。

Changing the type to 'text' instead of 'entity' replaces the dropdown with a input-field.. So, what's the best approach here?将类型更改为“文本”而不是“实体”会用输入字段替换下拉列表。那么,这里最好的方法是什么?

$formMapper
    ->add('post', 'entity', 
        array(
            'label' => 'Some post',
            'read_only' => true,
            'disabled'  => true,
            'class' => 'Acme\DemoBundle\Entity\Post'
        )
    )
;

This answer tells how to customize list rendering. 这个答案讲述了如何自定义列表渲染。 Maybe the same approach works with form rendering? 也许相同的方法适用于表单渲染?

If not, then you can create your custom form type according to create custom field type documentation , and customizing the template. 如果没有,则可以根据创建自定义字段类型文档和自定义模板来创建自定义表单类型。

This is a bit old but this might help someone.这有点旧,但这可能会对某人有所帮助。

Here is the code that resolves your issue.这是解决您的问题的代码。

$formMapper
->add('post', 'entity', array('label' => 'Some post','attr' => array(
                    'readonly' => 'readonly',
                    'disabled' => 'disabled',
                ),
                'class' => 'Acme\DemoBundle\Entity\Post')
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM