title: Symfony 2.7: Automatically Populate Form Field from Within Form Builder
body: <p>I have an entity named HoursSpecial with a foreign key relationship to an entity called HoursArea. Each HoursSpecial belongs to an HoursArea. When I create a new HoursSpecial via my HoursSpecialType, I want the form field to automatically populate the HoursArea field.</p>
  
  <p>I know what you're thinking, just do something like this in my controller's method:</p>
  
  <pre><code>$form-&gt;add('area', 'hidden', array('data'=&gt;$area));
  </code></pre>
  
  <p>That would be fine except I need to make a DataTransformer to switch between the area's ID and the actual area entity. So I have to declare my HoursArea field within my HoursSpecialType with the transformer:</p>
  
  <pre><code>    $builder
          ...
          -&gt;add('area', 'hidden')
      ;
  
      $builder-&gt;get('area')-&gt;addModelTransformer(new HoursAreaToIntTransformer($this-&gt;manager));
  </code></pre>
  
  <p>Now, I can't simply feed my HoursArea entity into the form. Is there an effective way to make this happen?</p>
  
  <p>I've thumbed through Symfony's documentation on <a href="http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-underlying-data" rel="nofollow">How to Dynamically Modify Forms Using Form Events</a>, but I can't make heads or tails of how I would pass in that HoursArea entity dynamically from outside of the form builder. Maybe I'm just missing something?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Following the recommendation of the answer (Recommendation #1) below from @Ryan, I have created the custom type HiddenHoursAreaType:</p>
  
  <pre><code>// AppBundle\Form\Type\HideenHoursAreaType.php
  class HiddenHoursAreaType extends AbstractType
  {
  //need to instantiate HoursAreaToIntTransformer
  private $manager;
  
  public function __construct(ObjectManager $manager)
  {
      $this-&gt;manager = $manager;
  }
  
  /**
   * @param FormBuilderInterface $builder
   * @param array $options
   */
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $transformer = new HoursAreaToIntTransformer($this-&gt;manager);
      $builder-&gt;addModelTransformer($transformer);
  }
  
  
  
  public function configureOptions(OptionsResolver $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; null,
          'compound' =&gt; true  //this should be FALSE as there are no children
      ));
  }
  
  /**
   * 
   * the return value of the getParent function indicates that you're extending the choice field type. 
   * This means that, by default, you inherit all of the logic and rendering of that field type.
   */
  public function getParent()
  {
      return 'hidden';
  }
  
  public function getName()
  {
      return 'app_hoursArea';
  }
  </code></pre>
  
  <p>I have added my transformer into the custom type class. Here is the transformer class:</p>
  
  <pre><code>// AppBundle\Form\DataTransformer;
  class HoursAreaToIntTransformer implements DataTransformerInterface
  {
      private $manager;
  
      public function __construct(ObjectManager $manager)
      {
          $this-&gt;manager = $manager;
      }
  
      /**
       * Transforms an object (HoursArea) to a string (number).
       *
       * @param  Issue|null $issue
       * @return string
       */
      public function transform($area)
      {
          if (null === $area) {
              return '';
          }
  
          return $area-&gt;getId();
      }
  
      /**
       * Transforms a string (number) to an object (HoursArea).
       *
       * @param  string $areaId
       * @return HoursArea|null
       * @throws TransformationFailedException if object (HoursArea) is not found.
       */
      public function reverseTransform($areaId)
      {
          // no area number? It's optional, so that's ok
          if (!$areaId) {
              return;
          }
  
          $area = $this-&gt;manager
              -&gt;getRepository('AppBundle:HoursArea')
              // query for the issue with this id
              -&gt;find($areaId)
          ;
  
          if (null === $area) {
              // causes a validation error
              // this message is not shown to the user
              // see the invalid_message option
              throw new TransformationFailedException(sprintf(
                  'An area with number "%s" does not exist!',
                  $areaId
              ));
          }
  
          return $area;
      }
  }
  </code></pre>
  
  <p>Within my controller I create the form with the custom type field:</p>
  
  <pre><code>$form = $this-&gt;createForm(new HoursSpecialType($this-&gt;getDoctrine()-&gt;getManager()), $entity, array(
          'action' =&gt; $this-&gt;generateUrl('hoursspecial_postcreate'),
          'method' =&gt; 'POST',
      ));
      $form-&gt;add('eventDate', 'hidden', array('data'=&gt;$dateString));
      $form-&gt;add('area', new \AppBundle\Form\Type\HiddenHoursAreaType($this-&gt;getDoctrine()-&gt;getManager()), array(
          'data'=&gt;$area,
          'invalid_message'=&gt;'Area field not converted proerly'
      ));
  
      $form-&gt;add('submit', 'submit', array('label' =&gt; 'Create'));
  </code></pre>
  
  <p>Thanks to the transformer and the custom type, the form now correctly converts the HoursArea entity to an integer for population in the hidden field.</p>
  
  <p><strong>The problem now is that upon form submission, the integer is not converted back into an HoursArea object. I know this because I get the 'invalid_message' upon submission.</strong></p>
  
  <p><strong>Final Update</strong></p>
  
  <p>The reason the HoursArea id wasn't being inserted properly had something to do with the </p>
  
  <pre><code>'compound' =&gt; true
  </code></pre>
  
  <p>setting I had in my custom type. I assume it was looking for child fields and wasn't finding any...which it shouldn't have because there were none!</p>