我创建了一个带有 Select 字段的表单,我需要在不同的操作中处理响应。动作“showInfo”显示游戏的信息并绘制一个表格,其中包含用户可加入游戏的角色列表。表单由“joinGame”操作处理,该操作在 url 中接收游戏的 slug,并通过表单接收角色的 id。我如何处理其他操作中的选定选项?
显示信息动作
/*...*/
$free_charact = $em->getRepository('PlayerBundle:Character')->findBy(
array(
'user' => $user,
'game' => null),
array()
);
/*...*/
if ($free_charact) {
$form = $this->createFormBuilder($free_charact)
->add('charact_join', 'choice', array(
'choices' => $array_select,
'multiple' => false,
))
->getForm();
$array_render['form'] = $form->createView();
return $this->render(
'GameBundle:Default:game_info.html.twig',
$array_render
);
加入游戏动作
/*...*/
$req = $this->getRequest();
if ($req->getMethod() == 'POST') {
$postData = $req->request->get('form_charact_join');
$id_charac = $postData[0];
$charac_change = $em->getRepository('PlayerBundle:Character')->findOneById($id_charac);
//Check if the character is property of the user
$charac_change->setGame($game);
$em->persist($charac_change);
$em->flush();
$this->get('session')->getFlashBag()->add('info', 'You are a player of this game now!');
}
return new RedirectResponse($this->generateUrl('info_game', array('slug' => $slug)));
game_info.html.twig
<form action="{{ path('join_game', {'slug': game.slug}) }}" method="post" {{ form_enctype(form) }}>
{{ form_errors(form) }}
{{ form_widget(form.charact_join) }}
<input type="submit" value="Join" />
</form>