Matt Welander
Matt Welander

Reputation: 8548

symfony2 twig referencing base widget

I am trying to customize my symfony2 form choice_widget_expanded part, and trying to reference the base (standard) for the choice_widget_collapsed as per this instruction http://symfony.com/doc/current/cookbook/form/form_customization.html

But I get an error saying

Notice: Undefined index: choice_widget_collapsed in /var/www/beta/app/cache/dev/twig/ff/8a/9f46c90c543b16e78e981aeda67b.php line 19

Here is my twig code:

{% extends '::base.html.twig' %}

{% use 'form_div_layout.html.twig' with choice_widget_collapsed as base_choice_widget_collapsed %}

{% form_theme form _self %}

{% block choice_widget %}
{% spaceless %}
    {% if expanded %}
        {{ block('choice_widget_expanded') }}
    {% else %}
        {{ block('base_choice_widget_collapsed') }}
    {% endif %}
{% endspaceless %}
{% endblock choice_widget %}


{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
    {% for child in form %}
        {{ form_widget(child) }}
        {{ form_label(child) }}
        <br />
    {% endfor %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}


{% block contents %}

Upvotes: 1

Views: 832

Answers (2)

Matt Welander
Matt Welander

Reputation: 8548

Ah.. I am sure Dylans answer is correct, but for the symfony2 version I use (2.0.16) the template for that form looks a little different, the choice_widget_expanded doesn't have it's own block but it is all in this

{% block choice_widget %}
{% spaceless %}
{% if expanded %}
    <div {{ block('widget_container_attributes') }}>
    {% for child in form %}
        {{ form_widget(child) }}
        {{ form_label(child) }}
        <br />          {# <-------- Here's my addition to the template #}
    {% endfor %}
    </div>
{% else %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
    {% if empty_value is not none %}
        <option value="">{{ empty_value|trans }}</option>
    {% endif %}
    {% if preferred_choices|length > 0 %}
        {% set options = preferred_choices %}
        {{ block('widget_choice_options') }}
        {% if choices|length > 0 and separator is not none %}
            <option disabled="disabled">{{ separator }}</option>
        {% endif %}
    {% endif %}
    {% set options = choices %}
    {{ block('widget_choice_options') }}
</select>
{% endif %}
{% endspaceless %}
{% endblock choice_widget %}

I Should of course have pulled the code out of my own codebase instead of grabbing the latest version off of github, doh!

Upvotes: 0

dylan oliver
dylan oliver

Reputation: 1284

This works for me on a fresh composer install of sf 2.2.1. You should not need to clear cache for template changes to register in the dev environment, but it's worth a shot. It could also be that you need to stop and start (not restart) apache/php5-fpm if using an opcode cache.

Since you're only modifying choice_widget_expanded all you really need is:

test.html.twig

{% extends '::base.html.twig' %}

{% form_theme form _self %}

{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
    {% for child in form %}
        {{ form_widget(child) }}
        {{ form_label(child) }}
        <br />
    {% endfor %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}

{% block body %}
<form>
{{ form_row(form.choice) }}
</form>
{% endblock %}

testAction

/**
 * @Template()
 */
public function testAction()
{
    $builder = $this->createFormBuilder();
    $builder->add('choice', 'choice', array(
            'choices'=>array('one'=>'one', 'two'=>'two', 'three'=>'three'),
            'expanded'=>true
    ));
    $form = $builder->getForm();
    return array('form'=>$form->createView());
}

Upvotes: 2

Related Questions