No default option is configured for constraint Symfony\Component\Validator\Constraints\DateTime Symfony2

datetime, php, symfony

Solution

Add a `\` before DateTime `new \DateTime('now')`. If you are not using the `\`, your DateTime is interpreted as `Symfony\Component\Validator\Constraints\DateTime` not the PHP `DateTime`.

You can remove `use Symfony\Component\Validator\Constraints\DateTime;` after that.

Problem

Hi I have this Exception : No default option is configured for constraint Symfony\Component\Validator\Constraints\DateTime. In My Controller i have : ``` use Symfony\Component\Validator\Constraints\DateTime; class DefaultController extends Controller { public function test2Action() { $em = $this->getDoctrine()->getEntityManager(); $id = 1; // ID du bureau de test que l'on a enregistré précédemment $desk = $this->getDoctrine()->getRepository('MytestBundle:Desk')->find($id); echo "Le bureau récupéré porte l'ID: ".$desk->getId()." et le titre: ".$desk->getTitle(); $comment = new DeskComment(); $comment->setDescrption("Mon premier commentaire: Joli bureau !"); $comment->setSubmissionIp($this->getRequest()->server->get('REMOTE_ADDR')); $comment->setCreateAt(new DateTime('now')); $comment->setDesk($desk); // On lie le commentaire à notre bureau d'ID 1 $em->persist($comment); // On persist le commentaire 1 $comment2 = new DeskComment(); $comment2->setDescrption("Mon deuxième commentaire: J'adore le bureau ! Bravo !"); $comment2->setSubmissionIp($this->getRequest()->server->get('REMOTE_ADDR')); $comment->setCreateAt(new DateTime('now')); $comment2->setDesk($desk); // On lie le commentaire à notre bureau d'ID 1 $em->persist($comment2); // On persist le commentaire 2 $em->flush(); // On sauvegarde en BDD les deux commentaires return $this->render('MytestBundle:Default:test2.html.twig'); } ``` I think the problem is new DateTime('now') Any help !!

Original source