Je valide mes données avant de les enregistrer dans la base de données, mais jusqu'à présent, lorsque la validation javascript est désactivée, je ne peux pas détecter si l'objet est valide ou non. Voici comment je le fais:symfony2 La méthode isValid() renvoie toujours vrai
$editForm = $this->createEditForm($post, $category_id, $section_id, $topic_id);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('topic_show', array('category_id'=> $category_id, 'section_id'=> $section_id, 'id'=> $topic_id)));
}
méthode unfortunatelly retourne isValid() vrai à chaque fois puis-je obtenir l'erreur de MySQL.
J'ai essayé approche différente, tirée de here
$validator = $this->get('validator');
$errors = $validator->validate($post);
if (count($errors) > 0) {
throw $this->createNotFoundException('Unable to find Post entity.');
/*
* Uses a __toString method on the $errors variable which is a
* ConstraintViolationList object. This gives us a nice string
* for debugging
*/
$errorsString = (string) $errors;
return new Response($errorsString);
}
Mais il est aussi ne fonctionne pas ...
MISE À JOUR
mon attribut 'contenu' qui est à l'origine de l'erreur
Avez-vous des [ 'Constraints'] (http://symfony.com/doc/current/book/validation.html#basic-constraints) dans votre' entité 'ou' FormType'? –
non, il semble que dans symfony tout attribut est requis par défaut – Leo
Non, vous devez utiliser ['NotBlank()'] (http://symfony.com/doc/current/reference/constraints/NotBlank.html) ou ['NotNull()'] (http://symfony.com/doc/current/reference/constraints/NotNull.html) pour rendre le champ obligatoire. –