title: Specify different validation groups for each item of a collection in Symfony 2?
body: <p>[<a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="noreferrer">Documentation about collection</a>] When embedding forms (collection type) is possible to specify validation groups for each item, based on the current item? It seems not working ATM.</p>
  
  <p>The <code>TaskType</code> form adding a collection of tags:</p>
  
  <pre class="lang-php prettyprint-override"><code>// src/Acme/TaskBundle/Form/Type/TaskType.php
  
  // ...
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      // ...
  
      $builder-&gt;add('tags', 'collection', array(
          // ...
          'by_reference' =&gt; false,
      ));
  }
  </code></pre>
  
  <p>For example we have two tags (tag 1 and tag 2) and a new tag is added using the "Add" button (via JavaScript):</p>
  
  <pre class="lang-none prettyprint-override"><code>-----------
  | add tag |
  -----------
  - tag 1 (existing)
  - tag 2 (added clicking the "add tag" button)
  </code></pre>
  
  <p>Tag 1 should be validated against <code>Default</code>, <code>Edit</code> groups while tag 2 against <code>Default</code> group only.</p>
  
  <h2><code>TagType</code> form defining dynamic validation groups</h2>
  
  <p>Based on the underlying data, if tag is new it gets <code>Default</code> group, if exists <code>Default</code>, <code>Create</code> groups:</p>
  
  <pre class="lang-php prettyprint-override"><code>// ...
  
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'validation_groups' =&gt; function (FormInterface $form) {
              $tag = $form-&gt;getData();
  
              $groups = array('Default');
              if (null !== $tag &amp;&amp; null !== $tag-&gt;getId()) {
                  $groups[] = 'Edit';
              }
  
              return $groups;
          }
      ));
  }
  
  // ...
  </code></pre>
  
  <h2><code>Tag</code> entity with a constraint in the "Edit" group</h2>
  
  <p>An example with <code>Tag</code> defining two properties (accessors omitted):</p>
  
  <pre class="lang-php prettyprint-override"><code>class Tag
  {
      /**
       * @Assert\NotBlank()
       */
      protected $name;
  
      /**
       * @Assert\NotBlank(groups={"Edit"})
       * @Assert\Length(max="255")
       */
      protected $description;
  
      // ...
  }
  </code></pre>
  
  <p>For an <strong>existing tag</strong>: description should not be blank. For a <strong>new tag</strong>: description can be blank.</p>
  
  <h2>Proof form is valid, validator shows errors (wrong!)</h2>
  
  <p>Just edit an existing tag and leave the description blank. The <strong>form validates</strong> but the <strong>validator service shows errors</strong>:</p>
  
  <pre class="lang-php prettyprint-override"><code>$form = $this-&gt;createForm('task', $task)
      -&gt;handleRequest($request);
  
  $validator = $this-&gt;get('validator');
  
  if ($form-&gt;isValid()) {
      foreach ($task-&gt;getTags() as $tag) {
          // Validate against Default, Edit groups
          $errors = $validator-&gt;validate($tag, array('Default', 'Edit'));
  
          if (null !== $tag &amp;&amp; null !== $tag-&gt;getId()) {
              echo 'Existing tag #'.$tag-&gt;getId();
          } else {
              echo 'New tag';
          }
  
          echo ', errors: '.count($errors).'&lt;br&gt;';
      }
  
      die('Form is valid.')
  
      // ...
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre class="lang-none prettyprint-override"><code>Existing tag #863, errors: 1
  Form is valid.
  </code></pre>
  
  <p><strong>Update 1</strong>: I've tried (without success) with a static method <code>determineValidationGroups</code> as suggested <a href="http://symfony.com/doc/current/book/forms.html#book-forms-validation-groups#groups-based-on-the-submitted-data" rel="noreferrer">here</a>:</p>
  
  <pre class="lang-php prettyprint-override"><code>public static function determineValidationGroups(FormInterface $form)
  {
      $groups =  array('Default');
      if ($form-&gt;getData() !== null &amp;&amp; null !== $form-&gt;getData()-&gt;getId())
      {
          $groups =  array('Edit');
      }
  
      var_dump($groups);
  
      return $groups;
  }
  </code></pre>
  
  <p>In <code>TagType</code> form:</p>
  
  <pre class="lang-php prettyprint-override"><code>/**
   * {@inheritdoc}
   */
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          // ... 
          'validation_groups' =&gt; array(
              'Acme\TaskBundle\Entity\Tag',
              'determineValidationGroups'
          ),
      ));
  }
  </code></pre>
  
  <p>Output with just one existing tag and one created using the "add tag" link seems correct. But no errors for the existing tag leaving the description blank:</p>
  
  <pre class="lang-none prettyprint-override"><code>array (size=1)
    0 =&gt; string 'Edit' (length=4)
  
  array (size=1)
    0 =&gt; string 'Edit' (length=4)
  
  rray (size=1)
    0 =&gt; string 'Default' (length=7)
  
  rray (size=1)
    0 =&gt; string 'Default' (length=7)
  </code></pre>