我们的Symfony 2项目中有一个奇怪的错误。我们使用Symfony表单,我们的开发环境没有任何问题,但在生产中接收错误消息。我们无法复制的错误,从未有过。当我们测试时(在prod和amp; dev中),一切正常,但我们确实收到了发生此错误的通知。
我们收到错误通知:
“exception”:“[object](Symfony \ Component \ Debug \ Exception \ FatalThrowableError(code:0):输入错误:参数1传递给Symfony \ Component \ Form \ FormRenderer :: renderBlock()必须是实例Symfony \ Component \ Form \ FormView,null
最重要的代码(为了紧凑性和可读性而剥离)
Formtype:
class WishlistType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('rank', HiddenType::class)
->add('description', TextType::class)
// Image is deprecated and will be removed.
->add('image');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'data_class' => Item::class,
]
);
}
}
控制器:
public function showAction(Request $request, $url)
{
/** @var Participant $participant */
$participant = $this->get('participant_repository')->findOneByUrl($url);
if ($participant === null) {
throw new NotFoundHttpException();
}
$wishlistForm = $this->createForm(
WishlistType::class,
$participant,
[
'action' => $this->generateUrl(
'wishlist_update',
['url' => $participant->getUrl()]
),
]
);
if (!$request->isXmlHttpRequest()) {
return [
'entry' => $participant,
'wishlistForm' => $wishlistForm->createView(),
];
}
}
Twig模板:
{{ form_start(wishlistForm, {'attr': {'id': 'add_item_to_wishlist_form'}}) }}
{{ form_row(wishlistForm._token) }}
<table>
<!-- ADD SOMETHING TO YOUR WISHLIST -->
<thead>
<tr>
<th>#</th>
<th>{{ 'entry_show_valid.label.description'|trans }}</th>
<th>
<button type="button" class="btn btn-mini btn-success add-new-entry">
{{ 'entry_show_valid.btn.add_wishlist'|trans }}
</button>
</th>
</tr>
</thead>
<!-- OVERVIEW OF ITEMS IN WISHLIST, AND POSSIBILITY TO REMOVE SINGLE ITEMS-->
<tbody>
{% for item in wishlistForm.wishlistItems %}
<tr>
<td>{{ form_widget(item.rank) }}
<span>{{ item.rank.vars.value }}</span>
</td>
<td>
{{ form_widget(item.description, {'attr': {'class': 'wishlistitem-description'} }) }}
</td>
<td>
<button type="submit" >
<span>{{ 'entry_show_valid.btn.update_item'|trans }}</span>
</button>
<button type="button">
<span>{{ 'entry_show_valid.btn.remove_item'|trans }}</span>
</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ form_end(wishlistForm, {'render_rest': false}) }}
表单处理是通过Ajax完成的,但请求是作为常规symfony表单处理的($wishlistForm->handleRequest($request);
等...)。
答案 0 :(得分:0)
我不明白你怎么能这样做:
{% for item in wishlistForm.wishlistItems %}
因为我没有看到任何'wishlistItems&#39;在您的表格声明中。 对我来说,这段代码必须在这一行失败......