Symfony2 -> Twig -> Form -> Field -> Set rendered = true

22,850

Solution 1

Am I missing the question here? If you want to set a field as rendered even though it is not the simple call is:

{% do form.x.setRendered %}

If I misunderstood, my apologies.

Solution 2

You can use next closing form statement to prevent rendering form fields which are defined in Form but not described in template:

{{ form_end(form, {'render_rest': false}) }}

For example, we define next form:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'id',
            HiddenType::class,
            array(                                     
                'required' => false
            )
        )
        ->add(
            'name',
            TextType::class,
            array(                                     
                'required' => false
            )
        )
        ->add(
            'comment',
            TextType::class,
            array(
                'required' => false
            )
        )
        ->add(
            'amount',
            TextType::class,
            array(
                'required' => false
            )
        );
}

For this form we describe next template, but we do not want to render field id, so we can use option render_rest in form_end block to ommit rendering of field id:

{# render opening form tag #}
{{ form_start(form) }}
{# render field with label #}
{{ form_row(form.name) }}
{# render only field #}
{{ form_widget(form.comment) }}
{# render only label #}
{{ form_label(form.amount) }}

{# render only field #}
{{ form_widget(form.amount) }}

{# if csrf token is enabled for form render field #}
{% if form._token is defined %}
{{ form_widget(form._token) }}
{% endif %}

{# render closing form tag and do not render rest form elements #}
{{ form_end(form, {'render_rest': false}) }}

Solution 3

You should remove ( or only add ) the form field in your FormType by including some kind of decision logic.

For example checking for existence/value of a cerain variable.

This variable could then be injected in the constructor.

Removing it from your template is application logic which does not belong into your template.

If have no other choice have a look at the FormView::setRendered() method.

You can access an object's methods using Twigs attribute function:

{{ attribute(object, method, arguments) }}
Share:
22,850
PatrickB
Author by

PatrickB

Updated on May 05, 2020

Comments

  • PatrickB
    PatrickB about 4 years

    i have a simple problem.

    I have a form with a field for example:

    $builder
        ->add('x')
        ->add('y')
        ->add('z')
    ;
    

    In my twig files i used multiple blocks and i want to stop render fields... I view the b.html.twig file!

    a.html.twig

    {% block body %}
        {% block form %}
            {{ form_widget(form) }}
        {% endblock form %}
    {% endblock body %}
    

    b.html.twig

    {% block form %}
        {{ form.x.set('rendered', true) | default() }}
        {{ parent() }}
    {% endblock form %}
    

    If i remove the "default()" i get the error, that the object cant be converted to a string. And actually the form renders all fields... Inclusive the x field. But they shouldnt render the x field...

    Thank you in advance!

  • PatrickB
    PatrickB almost 11 years
    The problem is, that i use a global form theme which contains the "form_widget(form)" as default. I need a field at the bottom and because that i would set rendered = true. Then i could use it at the bottom and my global-theme would not render the field...
  • Nicolai Fröhlich
    Nicolai Fröhlich almost 11 years
    updated my answer ... your exact question is how to call setRendered() for a row in a form manually ?
  • PatrickB
    PatrickB almost 11 years
    The question is how to do this with twig (only) ^^ At the moment i use a self-made twig function: public function renderState(FormView $field) { $field->setRendered(true); }
  • johnnyRose
    johnnyRose over 8 years
    This answer would be better if you added an explanation.
  • Tom
    Tom almost 8 years
    But this works for all fields. Also for _csrf_token SO you would need to remember to add _csrf field by hand
  • A.Seddighi
    A.Seddighi about 6 years
    this is a better answer but we have to remember to add {{ form_widget(form._token) }} to the template