I have a Form with 3 Layers.
The built in validation is somehow not triggered through all 3 levels. Therefore I have to check manually for Errors on the third level. If I find an error, the entity with the error is not persisted. A flush is always triggered at the end of the loop. Unfortunately even without being persisted, some invalid entities are saved in the database. Here is my Controller for it:
$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())
));
}
}
The persist operation is never triggered, but the invalid $bet entities are still saved in the database.