Symfony如何在各自的单独表单中编辑实体对象的数组

时间:2015-11-18 19:14:21

标签: symfony twig

我有一个从数据库查询的实体对象数组。我想在同一页面上使用每个对象旁边的投票提交按钮呈现其中的每个对象。我在twig模板中渲染它们没有问题,但更新操作会是什么样子?我似乎无法从那里弄明白。如果在我尝试更新的对象验证中出现错误,如何在同一页面上再次呈现所有表单,并在正确的表单旁边显示错误?我有问题,因为它是一系列表格。

这是呈现表单的操作:

 public function showVotingAction()
{
    $em = $this->getDoctrine()->getManager();
    $votes = $em->getRepository('PresidentialDebateBundle:Vote')
->findAll();

    $forms = array();

            foreach ($votes as $vote) {
          $forms[] = $this->createForm(
            new VoteType(),
            $vote,
            array(
                'action' => $this->generateUrl('vote'),
                'method' => 'POST',
            )
        )->createView();
    }

                return $this->render(
        'PresidentialDebateBundle:Voting:showVoting.html.twig',
        array(
            'forms' => $forms
        )
    );
}

这是树枝模板:

{% for form in forms %}
{{ form_start(form) }}
{{ form_errors(form) }}   
{{ form.vars.value.monthlySnack }}
{{ form.vars.value.count }}
{{ form_widget(form.vote) }}
<input type="hidden" name="voteId" value="{{ form.vars.value.id }}">
<input type="hidden" name="snackId" value="{{ form.vars.value.monthlySnack.snack }}">
{{ form_end(form) }}
{% endfor %}

这是我遇到问题的行动,并且无法弄清楚如何分别更新每一行:

public function voteAction(Request $request)
    {

    $voteId = $request->request->get("voteId");
    $em = $this->getDoctrine()->getManager();
    $vote = $em->getRepository("PresidentialDebateBundle:Vote")->find($voteId);

$form = $this->createForm(new VoteType(),$vote,array(
'action' => $this->generateUrl('vote'),
'method' => 'POST',));


if ($request->getMethod() == 'POST') {

$form->handleRequest($request);

if ($form->isValid()) {


$vote->setCount($vote->getCount() + 1);
$em->flush();

return $this->redirectToRoute('show_voting');// redirect when done

} 

}



return $this->render(
'PresidentialDebateBundle:Voting:showVoting.html.twig',
array('form' => $form->createView())
);

}

0 个答案:

没有答案