title: Symfony Conditional Form Validation
body: <p>I'm working on a Form in Symfony (2.6). The user can select a free product, which will be shipped to the user. The user has to fill in some personal details and his address (obligated). If he wants to specify another delivery address, he checks a checkbox that is not mapped to an Entity, and completes the delivery address. Now, I want to submit the form, and only validate the delivery address fields if the user has checked this checkbox. How can this be done?</p>
  <p>The address Fields and the Delivery Address Fields use the same Form Class mapped to the same Entity. I Use a YAML-file for my constraints.</p>
  <p>(part of) validation.yml:</p>
  <pre><code>AppBundle\Entity\Address:
      properties:
          street:
            - NotBlank: { message: &quot;Please fill in your first name.&quot; }
            - Length:
                min: 3
                max: 256
                minMessage: &quot;Please fill in your street name.&quot;
                maxMessage: &quot;Please fill in your street name.&quot;
          number:
            - NotBlank: { message: &quot;Please fill in your house number.&quot; }
            - Length:
                min: 1
                max: 10
                minMessage: &quot;Please fill in your house number.&quot;
                maxMessage: &quot;Please fill in your house number.&quot;
          postCode:
            - NotBlank: { message: &quot;Please fill in your postal code.&quot; }
            - Length:
                min: 2
                max: 10
                minMessage: &quot;Please fill in your postal code.&quot;
                maxMessage: &quot;Please fill in your postal code.&quot;
          city:
            - NotBlank: { message: &quot;Please fill in your city.&quot; }
            - Length:
                min: 2
                max: 256
                minMessage: &quot;Please fill in your city.&quot;
                maxMessage: &quot;Please fill in your city.&quot;
            - Type:
                type: alpha
                message: &quot;Please fill in your city.&quot;
          country:
            - NotBlank: { message: &quot;Please select your country.&quot; }
            - Country: ~
  
  AppBundle\Entity\Product:
      properties:
        product:
          - NotBlank: { message: &quot;Please select your product.&quot; }
          - Type:
              type: integer
              message: &quot;Please select your product.&quot;
        contact:
            - Type:
                type: AppBundle\Entity\Contact
            - Valid: ~
        deliveryAddress:
            - Type:
                type: AppBundle\Entity\Address
            - Valid: ~
  </code></pre>
  <p>Product Form Class:</p>
  <pre><code>    &lt;?php
  
  class ProductFormType extends AbstractType
  {
  
      /**
       *
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'AppBundle\Entity\Product'
          ));
      }
  
      /**
       * Returns the name of this type.
       *
       * @return string The name of this type
       */
      public function getName()
      {
          return 'product';
      }
  
  
      /**
       *
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('contact', new ContactFormType()); //CONTACTFORMTYPE also has an AddressFormType for the Address Fields
  
          $builder-&gt;add('differentDeliveryAddress', 'checkbox', array( //delivery address is only specific for this Form
              'label'     =&gt; 'Different Shipping Address',
              'required'  =&gt; false,
              'mapped'    =&gt; false
          ));
          $builder-&gt;add('deliveryAddress', new AddressFormType());
  
          //specific
  
          $builder-&gt;add('product', 'choice', array(
              'choices' =&gt; array('a'=&gt;'product x','b' =&gt; 'product y'),
              'required' =&gt; true,
              'invalid_message' =&gt; 'This field is required',
              'label' =&gt; 'Your Free Product',
          ));
  
          $builder-&gt;add('submit', 'button', array('label' =&gt; 'Submit'));
      }
  }
  </code></pre>
  <p>Finally the getProductFormAction in My Controller</p>
  <pre><code>   public function getProductFormAction(Request $request)
  {
  
      $product = new Product();
      $form    = $this-&gt;get('form.factory')-&gt;create(new ProductFormType($product);
  
      $form-&gt;handleRequest($request);
  
      if($form-&gt;isValid()){
          return new Response('Success'); //just to test
      }
  
      return $this-&gt;render(
          'productForm.html.twig',
          array(
              'pageTitle'   =&gt; 'Test',
              'form'        =&gt; $form-&gt;createView()
          )
      );
  }
  </code></pre>