我有一個表單來修改一個實體,它有一些子實體。我使用表單集合來做到這一點。當我編輯這個實體時,應該顯示子集合,但它不會。子集合由2個選擇字段和1個整數字段組成。整數字段使用正確的數據很好地呈現,但選擇字段要求選擇一個選項,而它應該顯示Matiere和Colle的子實體。選擇字段的表單集合
在代碼中,Colle實體是Matiere實體的子代。我使用MaterializeCSS作爲框架。
這裏是我的代碼:
子窗體:
$builder
->add('matiere', EntityType::class, [
'class' => 'PACESColleBundle:Matiere',
'attr' => ['class'=> 'matiere'],
'choice_label' => 'name',
'label' => false,
'required' => false,
'placeholder' => 'Choisissez une matière',
'mapped' => false])
->add('colleEnfant', EntityType::class, [
'class' => 'PACESColleBundle:Colle',
'attr' => ['class' => 'colles'],
'choice_label' => 'nom',
'label' => false,
'group_by' => 'matiere',
'required' => true,
'placeholder' => 'choose.colle'])
->add('ordre', IntegerType::class,[
'attr'=>['class'=>'ordre'],
'required' => true,
'label' => false]);
父窗體:
$builder->add('nom', TextType::class,['label' => 'Nom de la colle'])
->add('collesEnfants', CollectionType::class,
['label' => false,
'entry_type' => SousColleFormType::class,
'required' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false]);
查看:
<table id="tableau" class="creneaux"
data-prototype="{{ form_widget(form.collesEnfants.vars.prototype)|e }}">
<thead>
<tr>
<th>Matière</th>
<th>Colle</th>
<th>Ordre</th>
<th>Supprimer</th>
</tr>
</thead>
<tbody>
{% for colle in form.collesEnfants %}
<tr>
<td>{{ form_row(colle.matiere) }}</td>
<td>{{ form_row(colle.colleEnfant) }}</td>
<td>{{ form_row(colle.ordre) }}</td>
<td><a href="" class="delete_colle_link"><i class="material-icons">delete</i></a></td>
</tr>
{% endfor %}
</tbody>
</table>
<script>
$(document).ready(function() {
$('.matiere').material_select();
$('.colles').material_select()
});
</script>
場渲染是好的?你只是想知道爲什麼現有的數據沒有在選擇框中選擇? – Jeet
字段渲染是可以的。正如你所說的,唯一的問題是選擇字段中沒有選擇的現有數據 –