Symfony表单集合Twig渲染

时间:2016-07-26 07:29:47

标签: php jquery forms symfony twig

我的编辑表单上的表单集合的twig呈现有问题。 表单加载时没有任何问题,但是集合字段的标签加倍并从选项修改为数字,jsfiddle将显示我的意思。

  1. jsfiddle output of the html
  2. Also a screenshot
  3. 树枝模板:

    {% 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:

    {% 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 %}
    

3 个答案:

答案 0 :(得分:2)

    ->add('answerId', CollectionType::class, [
        'entry_options' => [
          'label' => false,
        ],
        'entry_type' => AnswerType::class,
        'allow_add' => true,
        'allow_delete' => true,
        'label' => false,
        'by_reference' => false,
    ])

这应该做的工作

答案 1 :(得分:1)

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

答案 2 :(得分:0)

在表单类型

中将label设置为false

示例:

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