简体   繁体   中英

Symfony Form Collection Twig Rendering

I have a problem with my twig rendering for form collection on edit form. The form loads with no problems but the collection field has the label doubled and modified from option to numbers the jsfiddle will show what i mean.

  1. jsfiddle output of the html
  2. Also a screenshot

Twig Template:

{% block body %}
    <div class="row">
        <div class="col-md-12">
            <a href="#" class="add-choice btn btn-default" aria-label="Add Choice">
                <span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span>
                Adauga Camp
            </a>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h1>Editeaza intrebarea</h1>
                </div>
                <div class="panel-body">
                    {{ form_start(edit_form, {'attr': {'class': 'form-horizontal'} } ) }}
                    <div class="row">
                        <div class="form-group">
                            {{ form_label(edit_form.question, null, {'label_attr': {'class': 'col-sm-2 control-label'} } ) }}
                            <div class="col-md-8">
                                {{ form_widget(edit_form.question, {'attr': {'class': 'form-control'} } ) }}
                                {{ form_errors(edit_form.question) }}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group">
                            {{ form_label(edit_form.surveyId, null, {'label_attr': {'class': 'col-sm-2 control-label'} } ) }}
                            <div class="col-md-8">
                                {{ form_widget(edit_form.surveyId, {'attr': {'class': 'form-control'} } ) }}
                                {{ form_errors(edit_form.surveyId) }}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group">
                            {{ form_label(edit_form.fieldType, null, {'label_attr': {'class': 'col-sm-2 control-label'} } ) }}
                            <div class="col-md-8">
                                {{ form_widget(edit_form.fieldType, {'attr': {'class': 'form-control'} } ) }}
                                {{ form_errors(edit_form.fieldType) }}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group">
                            {{ form_label(edit_form.categoryId, null, {'label_attr': {'class': 'col-sm-2 control-label'} } ) }}
                            <div class="col-md-8">
                                {{ form_widget(edit_form.categoryId, {'attr': {'class': 'form-control'} } ) }}
                                {{ form_errors(edit_form.categoryId) }}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group">
                            <div class="col-md-8">
                                {{ form_widget(edit_form._token, {'attr': {'class': 'form-control'} } ) }}
                                {{ form_errors(edit_form._token) }}
                            </div>
                        </div>
                    </div>
                    <div class="multiple-choice-prototype"
                         data-prototype="{{ form_widget(edit_form.answerId.vars.prototype)|e('html_attr') }}">
                        {{ form_widget(edit_form.answerId) }}
                    </div>
                    {{ form_widget(edit_form) }}
                    <div class="row">
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" value="Edit" class="btn btn-default">Salveaza</button>
                            </div>
                        </div>
                    </div>
                    {{ form_end(edit_form) }}
                    <div class="row">
                        <div class="col-md-12">
                            <div class="btn-group">
                                {{ form_start(delete_form) }}
                                <a class="btn btn-default" href="{{ path('surveyquestion_index') }}">Inapoi</a>
                                <input class="btn btn-default" type="submit" value="Sterge">
                                {{ form_end(delete_form) }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

Twig for the field:

{% block _survey_question_answerId_entry_widget %}
    <div class="row multiple-choice-container">
        <div class="form-group">
            {{ form_label(form.value, null, {'label_attr': {'class': 'col-sm-2 control-label'} } ) }}
            <div class="col-md-8">
                <div class="input-group">
                    <div class="input-group-addon">
                        <span class="glyphicon glyphicon-record" aria-hidden="true"></span>
                    </div>
                    {{ form_widget(form.value, {'attr': {'class': 'form-control'} } ) }}
                    {{ form_errors(form.value) }}
                </div>
            </div>
            <div class="col-md-2">
                <div class="btn-group">
                    <a href="#" class="add-choice btn btn-default" aria-label="Add Choice">
                        <span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span>
                    </a>
                    <a href="#" class="remove-choice btn btn-default" aria-label="Remove Choice">
                        <span class="glyphicon glyphicon glyphicon-minus" aria-hidden="true"></span>
                    </a>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
    ->add('answerId', CollectionType::class, [
        'entry_options' => [
          'label' => false,
        ],
        'entry_type' => AnswerType::class,
        'allow_add' => true,
        'allow_delete' => true,
        'label' => false,
        'by_reference' => false,
    ])

This should do the job

删除{{ form_label(form.value, null, {'label_attr': {'class': 'col-sm-2 control-label'} } ) }}

Set label to false in your form type

Example :

$builder->add('yourAttribute', null, array('label' => false));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM