title: Symfony2: How to use constraints on custom compound form type?
body: <p>Here is a question I've been breaking my head over for a while now. 
  Please know that I'm not a Symfony2 expert (yet), so I might have made a rookie mistake somewhere.</p>
  
  <p>Field1: Standard Symfony2 <code>text</code> field type</p>
  
  <p>Field2: Custom field type <code>compound</code>field with <code>text</code> field + <code>checkbox</code> field)</p>
  
  <p><img src="https://i.stack.imgur.com/Tjxfj.png" alt="preview"></p>
  
  <p><strong>My Goal:</strong> Getting constraints added to the <code>autoValue</code> field to work on the <code>autoValue's text input child</code></p>
  
  <p>The reason why the constraints don't work is probably because <code>NotBlank</code> is expecting a string value and the internal data of this form field is an array <code>array('input'=&gt;'value', 'checkbox' =&gt; true)</code>. This array value gets transformed back into a string with a custom <code>DataTransformer</code>. I suspect however that that happens AFTER validating the field against known constraints.</p>
  
  <p>As you see below in commented code, I have been able to get constraints working on the text input, however only when hardcoded into the autoValue's form type, and I want to validate against the main field's constraints.</p>
  
  <p>My (simplified) sample code for controller and field:</p>
  
  <p>.</p>
  
  <h2>Controller code</h2>
  
  <p>Setting up a quick form for testing purposes.</p>
  
  <pre><code>&lt;?php
  //...
  // $entityInstance holds an entity that has it's own constraints 
  // that have been added via annotations
  
  $formBuilder = $this-&gt;createFormBuilder( $entityInstance, array(
      'attr' =&gt; array(
          // added to disable html5 validation
          'novalidate' =&gt; 'novalidate'
      )
  ));
  
  $formBuilder-&gt;add('regular_text', 'text', array(
      'constraints' =&gt; array(
          new \Symfony\Component\Validator\Constraints\NotBlank()
      )
  ));
  
  $formBuilder-&gt;add('auto_text', 'textWithAutoValue', array(
      'constraints' =&gt; array(
          new \Symfony\Component\Validator\Constraints\NotBlank()
      )
  ));
  </code></pre>
  
  <p>.</p>
  
  <h2>TextWithAutoValue source files</h2>
  
  <p><strong>src/My/Component/Form/Type/TextWithAutoValueType.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace My\Component\Form\Type;
  
  use My\Component\Form\DataTransformer\TextWithAutoValueTransformer;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  
  class TextWithAutoValueType extends AbstractType
  {
      /**
       * {@inheritdoc}
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('value', 'text', array(
              // when I uncomment this, the NotBlank constraint works. I just
              // want to validate against whatever constraints are added to the
              // main form field 'auto_text' instead of hardcoding them here
              // 'constraints' =&gt; array(
              //     new \Symfony\Component\Validator\Constraints\NotBlank()
              // )
          ));
  
          $builder-&gt;add('checkbox', 'checkbox', array(
          ));
  
          $builder-&gt;addModelTransformer(
              new TextWithAutoValueTransformer()
          );
      }
  
      public function getName()
      {
          return 'textWithAutoValue';
      }
  }
  </code></pre>
  
  <p><strong>src/My/Component/Form/DataTransformer/TextWithAutoValueType.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace My\Component\Form\DataTransformer;
  
  use Symfony\Component\Form\DataTransformerInterface;
  
  class TextWithAutoValueTransformer 
      implements DataTransformerInterface
  {
      /**
       * @inheritdoc
       */
      public function transform($value)
      {
          return array(
              'value'    =&gt; (string) $value,
              'checkbox' =&gt; true
          );
      }
  
      /**
       * @inheritdoc
       */
      public function reverseTransform($value)
      {
          return $value['value'];
      }
  }
  </code></pre>
  
  <p><strong>src/My/ComponentBundle/Resources/config/services.yml</strong></p>
  
  <pre><code>parameters:
  
  services:
      my_component.form.type.textWithAutoValue:
          class: My\Component\Form\Type\TextWithAutoValueType
          tags:
              - { name: form.type, alias: textWithAutoValue }
  </code></pre>
  
  <p><strong>src/My/ComponentBundle/Resources/views/Form/fields.html.twig</strong></p>
  
  <pre><code>{% block textWithAutoValue_widget %}
      {% spaceless %}
  
      {{ form_widget(form.value) }}
      {{ form_widget(form.checkbox) }}
      &lt;label for="{{ form.checkbox.vars.id}}"&gt;use default value&lt;/label&gt;
  
      {% endspaceless %}
  {% endblock %}
  </code></pre>
  
  <p>.</p>
  
  <h2>Question</h2>
  
  <p>I have been reading docs and google for quite some hours now and can't figure out how to copy, bind, or reference the original constraints that have been added while building this form.</p>
  
  <p>-> Does anyone know how to accomplish this?</p>
  
  <p>-> For bonus points; how to enable the constraints that have been added to the main form's bound entity? (via annotations on the entity class)</p>
  
  <p><strong>PS</strong></p>
  
  <p>Sorry it became such a long question, I hope that I succeeded in making my issue clear. If not, please ask me for more details!</p>