Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form theming a collection widget

Tags:

twig

symfony

I have a collection widget in my form. That's displayed like:

Teams 0 player1 inputfield
1 player2 inputfield

I would like to not display the word 'teams' and the '0' and the '1'. I've got this block in my fields.html.twig template, but not really sure how to edit this.

{% block collection_widget %}
{% spaceless %}
    {% if prototype is defined %}
        {% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
    {% endif %}
    {{ block('form_widget') }}
{% endspaceless %}
{% endblock collection_widget %}

{% block form_label %}
{% spaceless %}
    <div class="hidden">
        {{ block('generic_label') }}
    </div>
{% endspaceless %}
{% endblock form_label %}

ChallengeType form:

class ChallengeType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('teams', 'collection', array(
                'type' => new TeamType(),
                'allow_add' => true
            ))
            ->add('place')
            ->add('date');
    }

    public function getName()
    {
        return 'challenge';
    }

    public function getDefaultOptions(array $options)
    {
        return array('data_class' => 'Tennisconnect\DashboardBundle\Entity\Challenge');
    }
}

Thx.

like image 968
mattyh88 Avatar asked Mar 18 '12 14:03

mattyh88


1 Answers

Those lables are created in form_label block. I usually wrap them in a div and set them hidden when needed.

Edit:

There is a better solution :).

Change collection section of the ChallengeType.php with following

->add('teams', 'collection', array(
                'type' => new TeamType(),
                //label for Teams text
                'attr' => array('class' => 'team-collection'),
                //label for each team form type
                'options' => array(
                  'attr' => array('class' => 'team-collection')
                ),
                'allow_add' => true
            ))

Now those unwanted labels will have team-collection class. In your css file you can set display:none for label.team-collection. No need to change form theme block definition.

like image 84
Mun Mun Das Avatar answered Oct 12 '22 11:10

Mun Mun Das