1
votes

I use EasyAdmin in Symfony 4 and I want to set some fields in twig template to disabled true or false depends on user role.

For example

{{ dump(form.role.vars.disabled) }}

shows true

I want to set it to false

{% block entity_form %}
    {% set form.role.vars.disabled = false %}
    {{ form(form) }}
{% endblock entity_form %}

But I got the error

Unexpected token "punctuation" of value "." ("end of statement block" expected).

Also I tried to merge it as array but got error.

How to do it correctly?

1
why not do that in your form configuration?yceruto

1 Answers

0
votes

You need to use the merge filter to update values of an array or a hash. You have a deeply nested hash so you need to use the merge filter many times:

{% set form = form|merge({
    role: form.role|merge({
        vars: form.role.vars|merge({
            disabled: false
        })
    })
}) %}

See TwigFiddle. (I used {{ var ? 'true' : 'false' }} instead of {{ dump(var) }} because TwigFiddle doesn't support the dump function.)

Update:

The above code doesn't work in your case because the merge filter converts the FormView object to an array. You would need to create a Twig extension to change the object's property. Take a look at this similar question: Set value of single object in multidimensional array in twig template

Or a better approach might be to do this kind of thing in the controller (or wherever you are configuring the form) like @yceruto suggested.