3つのレイヤーを持つフォームがあります。
組み込みの検証は、何らかの理由で 3 つのレベルすべてでトリガーされません。したがって、第 3 レベルのエラーを手動で確認する必要があります。エラーが見つかった場合、エラーのあるエンティティは保持されません。フラッシュは常にループの最後でトリガーされます。残念ながら、永続化されなくても、一部の無効なエンティティがデータベースに保存されます。これが私のコントローラーです:
$form = $this->createForm(new GameListType(), $betRound);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bind($request);
$betRound = $form->getData();
if ($form->isValid()) {
foreach ($betRound->getGames() as $game) {
if ($game->hasBet()) {
$bet = $game->getBet();
// Filter are used during select
// , but dont work for inserts
// should work in this current context!
$bet->setGame($game);
$bet->setBetRound($betRound);
$bet->setUser($user);
$validator = $this->container->get('validator');
$errors = $validator->validate($bet);
if (count($errors) == 0) {
print($bet. ' got persisted'); <-- never triggered
$em->persist($bet);
} else {
// Manual Error Handling
// (no cascade Validation to third level
foreach ($errors as $violation) {
$form->addError(new FormError(
$violation->getMessageTemplate(),
$violation->getMessageParameters(),
$violation->getMessagePluralization()
));
}
}
}
} // foreach ($betRound->getGames() as $game)
$em->flush();
if(!$form->hasErrors()){
return $this->redirect($this->generateUrl('betround_show',
array('id' => $betRound->getId())
));
}
}
永続化操作はトリガーされませんが、無効な $bet エンティティは引き続きデータベースに保存されます。