Symfony2 Sonata Admin show attribute only as a readyonly text
Asked Answered
R

3

12

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?

Ramp answered 16/9, 2013 at 8:22 Comment(0)
R
22
$formMapper
    ->add('post', 'entity', 
        array(
            'label' => 'Some post',
            'read_only' => true,
            'disabled'  => true,
            'class' => 'Acme\DemoBundle\Entity\Post'
        )
    )
;
River answered 5/11, 2013 at 10:23 Comment(0)
Z
1

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.

Zealotry answered 16/9, 2013 at 10:26 Comment(1)
The customize list rendering looks quite nice, but what about edit mode? I couldn't add a template for editing, and for a new rendering type...Ramp
D
0

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')
)
Dorree answered 6/10, 2021 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.