【发布时间】:2012-05-01 08:11:48
【问题描述】:
我刚开始使用 Symfony2 。我决定用 php 来实现我的表单,而不是学习 twig。我按照书中的说明进行操作,如下:
在控制器中:
public function editAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('MyBundle:MyEntity')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find MyEntity entity.');
}
$editForm = $this->createForm(new MyEntityType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render('MyBundle:MyEntity:edit.html.php' array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView()
));
}
在edit.html.php中:
<form action="<?php echo $view['router']->generate('myentity_update', array('id'=>$entity->getId()))?>" method="post" <?php echo $view['form']->enctype($editForm) ?>>
<?php echo $view['form']->widget($editForm)?>
<p>
<button type="submit">Save Changes</button>
</p>
</form>
<ul class="record_actions">
<li>
<a href="<?php $view['router']->generate('myentity') ?>">
Back to the list
</a>
</li>
<li>
<form action="<?php $view['router']->generate('myentity_delete', array('id'=>$entity->getId()) ) ?>" method="post">
<?php echo $view['form']->widget($deleteForm)?>
<button type="submit">Delete</button>
</form>
</li>
当我尝试显示表单时出现错误: 注意:未定义变量editForm
据我所知,我完全按照The Book 中的说明进行操作。我已经尝试在操作和模板中用 $form 替换 $editForm 或在模板中用 edit_form 替换 $editForm - 不用说,这些都不起作用。
我非常感谢任何关于我接下来应该尝试什么的建议(或者我可能不得不放弃学习 twig,因为至少相关说明似乎是正确的)。
【问题讨论】:
-
你能添加控制器和html的完整代码吗?
-
添加了控制器中editAction函数的所有代码和模板的完整代码
-
@richsage 哎呀,抱歉 - 现在完成了。不知道为什么我之前从未注意到答案旁边的勾号