mattyh88
mattyh88

Reputation: 1635

Form theming a collection widget

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.

Upvotes: 6

Views: 5536

Answers (1)

Mun Mun Das
Mun Mun Das

Reputation: 15002

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.

Upvotes: 5

Related Questions