我想在 symfony2 中对字段进行分组。例如,将它们包装在一个 div 中,并将标题放在它们之间:
<form>
<div class="step-1">
<h3>Step 1</h3>
Field 1
Field 2
</div>
<div class="step-2">
<h3>Step 2</h3>
Field 3
Field 4
</div>
</form>
问题是表单有很多字段,所以我无法在模板中一一呈现它们。添加字段时没有任何选项吗?喜欢:
$form = $this->createFormBuilder()
->addGroup('step-1')
或者我该如何处理?
最佳答案
我发现,根据this post (感谢“n.1”的链接)可以在 Controller 内分组:
$form = $this->createFormBuilder()
->add(
$this->createFormBuilder()->create('step1', 'form', array('virtual' => true))
->add('field1', 'text')
->add('field2', 'text')
)
给出以下模板:
<div class="input-wrapper">
<label class="required">Step1</label>
<div id="form_step1">
<div class="input-wrapper">
<label class="required" for="form_step1_field1">Field1</label>
<input id="form_step1_field1" type="text" required="required" name="form[step1][field1]">
<input id="form_step1_field2" type="text" required="required" name="form[step1][field1]">
</div>
</div>
</div>
我可以按照我想要的方式设置主题。而且“bschussek”写道:
The structure in your form class shouldn't necessarily be related to the structure in your layout. You can structure the fields in the HTML in whichever way you like.
所以也许最好的做法是不使用 Controller 进行结构化,我应该更喜欢“n.1”中的做法
关于php - Symfony2 中的组表单域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20600595/