我在Challenge-controller的“new-action”中有这个代码。当我提交Challenge-form时,它会抛出错误“field player_one not exists”。
if ($form->isValid())
{
foreach($form->get('teams') as $team_form)
{
$player_one = $team_form->get('player_one')->getData();
$player_two = $team_form->get('player_two')->getData();
$user_one = $this->getDoctrine()->getRepository('TennisconnectUserBundle:User')->findOneByUsername($player_one);
$user_two = $this->getDoctrine()->getRepository('TennisconnectUserBundle:User')->findOneByUsername($player_two);
// Add user(s) to the two teams
foreach($challenge->getTeams() as $i => $team)
{
// First team
if($i == 0)
{
if($user_one)
{
$team->addUser($user_one);
} elseif($user_two)
{
$team->addUser($user_two);
}
}
// Second team
elseif($i == 1)
{
if($user_one)
{
$team->addUser($user_one);
} elseif($user_two)
{
$team->addUser($user_two);
}
}
}
}
$em = $this->getDoctrine()->getEntityManager();
$em->persist($challenge);
$em->flush();
return $this->render('TennisconnectDashboardBundle:Default:index.html.twig', array('form' => $form->createView(), 'user' => $user));
}
但是当我调试“$ player_one”时,它会从文本框中给出正确的值:
$player_one = $team_form->get('player_one')->getData();
die($player_one);
答案 0 :(得分:0)
发现问题。 当我这样做时,它给了我3个结果,而我只是在挑战表中添加了2个团队表格。
die(count($form->get('teams')));
我不得不像这样删除csrf令牌
$form->get('teams')->remove('_token');