Symfony 表单集合 Twig 渲染
Posted
技术标签:
【中文标题】Symfony 表单集合 Twig 渲染【英文标题】:Symfony Form Collection Twig Rendering 【发布时间】:2016-11-29 17:36:02 【问题描述】:我的 Twig 渲染在编辑表单上的表单集合时遇到问题。 表单加载没有问题,但集合字段的标签加倍并从选项修改为数字,jsfiddle 将显示我的意思。
-
jsfiddle output of the html
Also a screenshot
树枝模板:
% 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 %
田间树枝:
% 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 %
【问题讨论】:
【参考方案1】:删除 form_label(form.value, null, 'label_attr': 'class': 'col-sm-2 control-label' )
【讨论】:
是的,这部分有效,但由于我的表单与新操作共享,如果我删除标签,我将不会在新操作上设置标签,我真的不想拥有我的标签命名为 0 , 1 , 2 , 3 删除它也不会删除编号。现在做了一个测试:D【参考方案2】:在表单类型中将标签设置为 false
例子:
$builder->add('yourAttribute', null, array('label' => false));
【讨论】:
【参考方案3】: ->add('answerId', CollectionType::class, [
'entry_options' => [
'label' => false,
],
'entry_type' => AnswerType::class,
'allow_add' => true,
'allow_delete' => true,
'label' => false,
'by_reference' => false,
])
这应该可以完成工作
【讨论】:
以上是关于Symfony 表单集合 Twig 渲染的主要内容,如果未能解决你的问题,请参考以下文章