Validation is done well when I modify an existing object, it does not work in the creation
<p>I worry a validation does not work when I create a new object "resa" but works if I change an existing object.</p>

<p>I can not find where it comes !?</p>

<p>My entity:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php

namespace Damias\ResaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Damias\ResaBundle\Validator\Constraints as ResaAssert;

/**
 * Reservation
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Damias\ResaBundle\Entity\ReservationRepository")
 * @ResaAssert\ResaUnique
 */
class Reservation
{
...
</code></pre>

<p>My constraint:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php

namespace Damias\ResaBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class ResaUnique extends Constraint
{
    public $message = 'Réservation en conflit (chambre déjà occupée dans cette période)';

    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

    public function validatedBy()
    {
        return 'damiasresa_resaunique';
    }
}
</code></pre>

<p>My validator:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php

namespace Damias\ResaBundle\Validator\Constraints;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class ResaUniqueValidator extends ConstraintValidator
{
    private $requestStack;
    private $em;

    public function __construct(RequestStack $requestStack, EntityManagerInterface $em)
    {
        $this-&gt;requestStack=$requestStack;
        $this-&gt;em=$em;
    }

    public function validate($value, Constraint $constraint)
    {
        $dateArrivee=clone $value-&gt;getDateArrivee();
        $dateArrivee-&gt;modify('+1 day');
        $dateDepart=clone $value-&gt;getDateDepart();
        $dateDepart-&gt;modify('-1 day');
        $ressources=$value-&gt;getRessources()-&gt;getValues();
        $id_ressources=array();
        foreach ($ressources as $ressource)
        {
            $id_ressources[]=$ressource-&gt;getId();
        }
        $autre_resa=$this   -&gt;em
                            -&gt;getRepository('DamiasResaBundle:Reservation')
                            -&gt;autreResa($id_ressources, $dateArrivee, $dateDepart);

        if (count($autre_resa)&gt;1)
        {
                $this-&gt;context  -&gt;buildViolation($constraint-&gt;message)
                                -&gt;atPath('Ressources')
                                -&gt;addViolation();
        }
    }
}
</code></pre>

<p>My editing action that works:</p>

<pre class="lang-php prettyprint-override"><code>    public function reservationAction($id)
    {
        // SESSION
        $session = $this-&gt;get('session');

        $em = $this -&gt;getDoctrine()
                    -&gt;getManager();
        $resa = $em -&gt;getRepository('DamiasResaBundle:Reservation')-&gt;find($id);

        $form_reservation = $this   -&gt;createForm(new ReservationType, $resa);

        $request = $this-&gt;get('request');
        if($request-&gt;getMethod()=='POST')
        {            
            //Récupération de la Réservation - Modification
            if($this -&gt;get('request') -&gt;get('damias_resabundle_reservation')!==null)
            {
                $form_reservation-&gt;handleRequest($request);
                if ($form_reservation-&gt;isSubmitted() &amp;&amp; $form_reservation-&gt; isValid())
                {
                    $em=$this-&gt;getDoctrine()-&gt;getManager();
                    $em-&gt;flush();
                    $date=$resa-&gt;getDateArrivee();
                    $session-&gt;set('date', $date);
                    $response = $this-&gt;redirect($this-&gt;generateUrl('damias_resa_homepage'));
                    return $response;
                }
            }
        }

        $response = $this-&gt;render(
            'DamiasResaBundle:Default:reservation_edit.html.twig',
            array(
                'form_reservation' =&gt; $form_reservation-&gt;createView(),
                'resa' =&gt; $resa
            )
        );
        return $response;
    }
</code></pre>

<p>My additional action that does not take into account validation:</p>

<pre class="lang-php prettyprint-override"><code>    public function ajouter_reservationAction()
    {
        // SESSION
        $session = $this-&gt;get('session');
        if ($session-&gt;get('resa')!=null) {
            $resa = new Reservation($session-&gt;get('resa'));
        }
        else {
            $resa = new Reservation();
        }
        $client = new Client();
        $request = $this-&gt;get('request');

        $form_client = $this        -&gt;createForm(new ClientType,$client);
        $form_reservation = $this   -&gt;createForm(new ReservationType, $resa);

        // Récupération des choix
        if($request-&gt;getMethod()=='GET')
        {
            // Récupération de la date de début
            if($this -&gt;get('request') -&gt; get('choix') !==null)
            {
                $choix=$this -&gt;get('request') -&gt; get('choix');
                $date_arrivee=$choix['date_encours']['date'];
                $date_arrivee = new \Datetime($date_arrivee);
                $date_depart = clone $date_arrivee;
                $date_depart-&gt;modify('+1 day');
                $ressourceId=$choix['ressource'];
                $em = $this -&gt;getDoctrine()
                            -&gt;getManager();
                $ressource = $em-&gt;getRepository('DamiasResaBundle:Ressource')-&gt;find($ressourceId);
                $id_categories=$choix['categories'];
                $resa   -&gt;setDateArrivee($date_arrivee)
                        -&gt;setDateDepart($date_depart)
                        -&gt;addRessource($ressource);
                $session-&gt;set('resa', $resa);

                $categories = $ressource-&gt;getCategories();

                $form_reservation = $this   -&gt;createForm(new ReservationType, $resa);
            }
        }
        // Récupération du formulaire
        if($request-&gt;getMethod()=='POST')
        {            
            // Récupération du nouveau client
            if($this -&gt;get('request') -&gt;get('damias_resabundle_client')!==null)
            {
                if ($form_client-&gt;handleRequest($request)-&gt; isValid())
                {
                    $em=$this-&gt;getDoctrine()-&gt;getManager();
                    $em-&gt;persist($client);
                    $em-&gt;flush();
                    $resa-&gt;setClient($client);
                    $session-&gt;set('resa', $resa);
                }
            }

            //Récupération de la Réservation
            if($this -&gt;get('request') -&gt;get('damias_resabundle_reservation')!==null)
            {
                $form_reservation-&gt;handleRequest($request);
                if ($form_reservation-&gt;isSubmitted() &amp;&amp; $form_reservation-&gt; isValid())
                {
                    $em=$this-&gt;getDoctrine()-&gt;getManager();
                    $em-&gt;persist($resa);
                    $em-&gt;flush();
                    $date=$resa-&gt;getDateArrivee();
                    $session-&gt;set('date', $date);
                    $session-&gt;set('resa', null);
                    $response = $this-&gt;redirect($this-&gt;generateUrl('damias_resa_homepage'));
                    return $response;
                }
            }
        }

        $response = $this-&gt;render(
            'DamiasResaBundle:Default:reservation_new.html.twig',
            array(
                'form_reservation' =&gt; $form_reservation-&gt;createView(),
                'form_client' =&gt; $form_client-&gt;createView()
            )
        );
        return $response;
    }
</code></pre>

<p>Validation is done well when I modify an existing object, it does not work in the creation! Where is that from?
An idea? a track?</p>

<p>Thanks for your help
And sorry about the formatting my code, this is my first post and I have not found how to do (I'm not very comfortable with the language of Shakespeare)</p>