Symfony2 Sonata Admin show attribute only as a readyonly text

假如想象 提交于 2020-01-12 03:31:09

问题


I have some immutable attributes on my entity to administrate with sonata-admin bundle.

I want to show them in the edit-view of the entity, but don't want to provide any mechanism to change it (e.g. 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. 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.

Changing the type to 'text' instead of 'entity' replaces the dropdown with a input-field.. So, what's the best approach here?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/18823401/symfony2-sonata-admin-show-attribute-only-as-a-readyonly-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!