I have a "Sonata_type_collection" field that only the owner can edit , and i want that the admin can only read this attribute ( he can edit other attributes). I couldn't find anything but this :
$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',
));
it works somehow , the attribute can't be edited(when the form is submitted an error message is shown) but the button "add" and the checkbox "delete" still and a dropdown can be edited at least in the view .
is there a way to do this ?
you can hide the buttons using btn_add = false
in the options array
https://sonata-project.org/bundles/admin/3-x/doc/reference/form_types.html#sonata-type-collection
but I will probably try to check in the frontend with twig, check if the user has certain role {% if is_granted('ROLE_ADMIN') %} ... {% endif %}
and enable or disable the form component.
I will probably do...
{% set disabled = !is_granted('ROLE_YOU_WANT_TO_ALLOW') %} // in your case ROLE_OWNER
and then when rendering try something like...
{{ form_row(yourForm.yourCollectionName, {
'disabled': disabled
}) }}
Take as an example the twig template reference
http://symfony.com/doc/current/reference/forms/twig_reference.html#form-variables-reference
That's an idea that may let you do what you want
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.