【发布时间】:2018-05-10 13:50:10
【问题描述】:
我知道我不是第一个提出这个问题的人。但我尽一切努力让它发挥作用。
起初,我有一个名为Assignment 的实体和另一个名为unit 的实体。 Assignment 与 Unit 具有多对多关系,称为 units。
这将创建表单:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('pickupTime', DateTimeType::class, [
'required' => true,
'label' => 'Zeitpunkt Abholung',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
])
->add('deliverTime', DateTimeType::class, [
'required' => true,
'label' => 'Zeitpunkt Lieferung (geplant)',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
])
->add('customer', EntityType::class, [
'required' => true,
'label' => 'Kunde',
'class' => Customer::class,
'query_builder' => function (EntityRepository $er) use ($options) {
return $er->createQueryBuilder('cu')
->leftJoin('cu.client', 'c')
->where('c.id = :cid')
->setParameter('cid', $options['client_id'])
->orderBy('cu.companyName', 'DESC');
},
])
->add('pickupAddress', AddressType::class, [])
->add('deliverAddress', AddressType::class, [])
->add('units', CollectionType::class, [
'entry_type' => UnitType::class,
'allow_add' => true,
'prototype' => true,
'attr' => array(
'class' => 'assignment-units',
),
]);
}
这是units的表单部分的模板:
<div class="row">
<div class="col">
{{ form_label(form.units) }}
<table class="table table-bordered table-light collection-form-wrapper">
<thead>
<tr>
<th>Bez.</th>
<th>Stück</th>
<th>Gewicht</th>
<th>Breite</th>
<th>Höhe</th>
<th>Tiefe</th>
<th></th>
</tr>
</thead>
<tbody>
{% for unit in form.units %}
<tr>
<td>{{ form_row(unit.name) }}</td>
<td>{{ form_row(unit.pieces) }}</td>
<td>{{ form_row(unit.weight) }}</td>
<td>{{ form_row(unit.width) }}</td>
<td>{{ form_row(unit.height) }}</td>
<td>{{ form_row(unit.depth) }}</td>
<td>
<a href="#" class="btn btn-danger btn-sm btn-delete" data-id="{{ loop.index }}"><i
class="fa fa-trash"></i></a>
</td>
</tr>
{% else %}
<tr>
<td colspan="7" class="text-center">
<div class="p2">
<i>
<small>Noch keine Einträge vorhanden</small>
</i>
</div>
</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td colspan="7">
<a href="#" class="btn btn-primary btn-sm" id="add-item"><i class="fa fa-plus"></i>
Hinzufügen</a>
<a href="#" class="btn btn-danger btn-sm" id="delete-all"><i class="fa fa-trash"></i> Tabelle leeren</a>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
{% form_theme form _self %}
{% block _units_entry_widget %}
<tr>
<td>{{ form_row(form.name) }}</td>
<td>{{ form_row(form.pieces) }}</td>
<td>{{ form_row(form.weight) }}</td>
<td>{{ form_row(form.width) }}</td>
<td>{{ form_row(form.height) }}</td>
<td>{{ form_row(form.depth) }}</td>
<td>
<a href="#" class="btn btn-danger btn-sm btn-delete"><i
class="fa fa-trash"></i></a>
</td>
</tr>
{% endblock %}
但它不起作用 - 它对应于 symfony 文档 (http://symfony.com/doc/master/form/form_customization.html#how-to-customize-a-collection-prototype) - 但我收到错误消息 Neither the property "name" nor one of the methods "name()", "getname()"/"isname()"/"hasname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".。
我怎么了?我不明白问题出在哪里。当我尝试使用 form_theme 覆盖 units 的小部件时,调试器显示集合 units 的任何字段。当我删除带有 form_theme 的部分时,会显示这些字段。
编辑: UnitType代码:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('quantity')
->add('weight')
->add('width')
->add('height')
->add('depth');
}
【问题讨论】:
-
我认为错误来自
form_row(unit.name)。您是否在UnitType中定义了name字段?您能否在您的问题中添加UnitType的代码? -
我也试过
form_row(unit.name),然后错误是Variable 'unit' does not exists.。我已经在任务中添加了代码。 -
如果我逐字遵循 Symfony 文档,我也会遇到同样的错误。你找到解决办法了吗?
标签: php forms symfony twig symfony-3.4