title: Tags using select2 and symfony/doctrine
body: <p>I try to use the Select2 jQuery plugin with Symfony's form component and Doctrine to create a tag field where I can add existing tags and create new ones on the fly.</p>
  
  <p>So far so good. I use the following Select2 init code:</p>
  
  <pre><code>$('select.tags').select2({
      tags: "true"
  });
  </code></pre>
  
  <p>All existing tags get loaded on each request. New one's should be created/persisted on submitting the whole form. (So no AJAX magic.)</p>
  
  <p>I don't know where I can hook in now to achieve this so that not existing tags get persisted to the database and added to my parent entity.</p>
  
  <p>It has to happend somewhere between:</p>
  
  <pre><code>if ($form-&gt;isSubmitted()) {
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>if ($form-&gt;isValid()) {
  }
  </code></pre>
  
  <p>in my controller. But I can't imagine what's the best way to do this (or if there is any). </p>
  
  <p><a href="http://symfony.com/doc/2.7/cookbook/form/form_collections.html" rel="nofollow">I read about form collection</a> but somehow this is not really what I need. Because there you have one single <code>input</code> field for each tag. But I have a <code>select</code> field with new options added dynamically.</p>