title: Symfony 3 | Custom constraints on CollectionType entities
body: <p>I followed the documentation and was able to add custom constraints on many of my fields (<a href="http://symfony.com/doc/current/validation/custom_constraint.html" rel="nofollow">http://symfony.com/doc/current/validation/custom_constraint.html</a>).</p>
  
  <p>I'm figuring a problem with CollectionType field.
  My custom constraint just check if user didn't tap multiple space in field (the constraint doesn't matter anyway).</p>
  
  <p><strong>I have a Question form with a title and answers :</strong></p>
  
  <pre><code>$builder
      -&gt;add('title', TextType::class)
      -&gt;add('answers', CollectionType::class, array(
              'entry_type'    =&gt; AnswerType::class,
              'allow_add'     =&gt; true,
              'allow_delete'  =&gt; true,
              'by_reference' =&gt; false
          ))
  </code></pre>
  
  <p><strong>I have my constraint :</strong></p>
  
  <pre><code>use Symfony\Component\Validator\Constraint;
  
  /**
   * @Annotation
   */
  class ContainsText extends Constraint
  {
      public $message = 'constraint_error';
  }
  </code></pre>
  
  <p><strong>And my constraint validator :</strong> </p>
  
  <pre><code>use Symfony\Component\Validator\Constraint;
  use Symfony\Component\Validator\ConstraintValidator;
  
  class ContainsTextValidator extends ConstraintValidator
  {
      public function validate($value, Constraint $constraint)
      {
          // It checks if user didn't had multiple space in field
          if (strlen(trim($value)) == 0) {
              $this-&gt;context-&gt;buildViolation($constraint-&gt;message)
                  -&gt;addViolation();
          }
      }
  }
  </code></pre>
  
  <p>In my entities :</p>
  
  <p><strong>Question:</strong></p>
  
  <pre><code>use XX\XXBundle\Validator\Constraints as CustomAssert;    
  
  class Question 
  {
      /**
       * @var string
       *
       * @ORM\Column(name="title", type="string", length=255, unique=true)
       * @CustomAssert\ContainsText    
       */
      private $title;
      ...
  }
  </code></pre>
  
  <p><strong>Answer :</strong></p>
  
  <pre><code>use XX\XXBundle\Validator\Constraints as CustomAssert; 
  
  class Answer 
  {
      /**
       * @var string
       *
       * @ORM\Column(name="text", type="string", length=255, unique=true)
       * @CustomAssert\ContainsText    
       */
      private $text;
      ...
  }
  </code></pre>
  
  <p>In my form validation, if in Question title I tap many spaces, I get a form validation error with my "constraint_error" message => Everything is working.</p>
  
  <p>But, if in Question answers text I tap many spaces, the form validation doesn't return any errors and my question is created with empty answers ! </p>
  
  <p>It seems that, if the field comes from a CollectionType, the custom asserts are ignored. </p>
  
  <p>What I don't understand is, if i had a Assert (like @Assert\Blank(), not a custom one) on answer text, even if we are in a CollectionType, the assert is not ignored and I can't validate a form with a blank answer.</p>
  
  <p>What did I miss here ? TY </p>