title: Symfony2 Data Transformer on hidden field
body: <p>I've searched and not found anyone with this problem. </p>
  
  <p>I've created my own Data Transformer as set out in the Cookbook and it all seems right but i get the error:</p>
  
  <blockquote>
    <p>The form's view data is expected to be an instance of class Niche\SecurityBundle\Entity\BusinessUser, but is a(n) integer. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) integer to an instance of Niche\SecurityBundle\Entity\BusinessUser.</p>
  </blockquote>
  
  <p>The transformer is below:</p>
  
  <pre><code>&lt;?php
  namespace Niche\SecurityBundle\Form\DataTransformer;
  
  use JMS\SecurityExtraBundle\Security\Util\String;
  
  use Symfony\Component\Form\DataTransformerInterface;
  use Symfony\Component\Form\Exception\TransformationFailedException;
  use Doctrine\Common\Persistence\ObjectManager;
  use Niche\SecurityBundle\Entity\BusinessUser;
  
  class BusinessUserToIdTransformer implements DataTransformerInterface
  {
  /**
   * @var ObjectManager
   */
  private $om;
  
  /**
   * @param ObjectManager $om
   */
  public function __construct(ObjectManager $om)
  {
      $this-&gt;om = $om;
  }
  
  /**
   * Transforms an object (BusinessUser) to a string (number)
   * 
   * @param BusinessUser|null $businessUser
   * @return String
   */
  public function transform($businessUser)
  {
      if (null === $businessUser) {
          return "";
      }
  
      return $businessUser-&gt;getId();
  }
  
  /**
   * Transforms a string (number) to an object (BusinessUser).
   * 
   * @param string $number
   * 
   * @return BusinessUser|null
   * 
   * @throws TransformationFailedException if object (BusinessUser) is not found.
   */
  public function reverseTransform($id)
  {
      if (!$id) {
          return null;
      }
  
      $businessUser = $this-&gt;om
          -&gt;getRepository('NicheSecurityBundle:BusinessUser')
          -&gt;findOneById($id);
  
      if (null === $businessUser) {
          throw new TransformationFailedException(sprintf(
                  'An issue with number "%s" does not exist!',
                  $number
                  ));
  
      }
              return $businessUser
  }
  }
  </code></pre>
  
  <p>and my form code is </p>
  
  <pre><code>&lt;?php
  
  namespace Niche\JobBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  use Niche\SecurityBundle\Form\DataTransformer\BusinessUserToIdTransformer;
  
  class JobType extends AbstractType
  {
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
  
      $entityManager = $options['em'];
      $transformer = new BusinessUserToIdTransformer($entityManager);
  
      //Get the BusinessUser object passed in.
      $businessUser = $options['businessUser'];
  
  
      $builder-&gt;add('title', 'text');
      $builder-&gt;add('jobDescription', 'textarea', array(
              "label" =&gt; "Job Description", )
          );
      $builder-&gt;add('companyDescription', 'textarea', array(
              "label" =&gt; "Company Description", )
          );
      $builder-&gt;add('salary', 'text');
      $builder-&gt;add('category', 'entity', array(
          'class' =&gt; 'NicheJobBundle:Category',
          'property' =&gt; 'name',   
      ));
      $builder-&gt;add('responsibilities', 'textarea');
      $builder-&gt;add('closingDate', 'datetime');
      //Add Business User to the form - Need a way for a job to be added by site admin in addition, could just be site admin users logged in with a Business user account
      $builder-&gt;add(
              $builder-&gt;create('company', 'hidden', array(
                  'data' =&gt; $businessUser,
  
              ))-&gt;addViewTransformer($transformer)
  
      );
  
  }
  
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'Niche\JobBundle\Entity\Job'
      ));
  
      $resolver-&gt;setRequired(array(
              'em', 'businessUser'
      ));
  
      $resolver-&gt;setAllowedTypes(array(
          'em' =&gt; 'Doctrine\Common\Persistence\ObjectManager',
      ));
  }
  
  public function getName()
  {
      return 'niche_jobbundle_jobtype';
  }
  }
  </code></pre>
  
  <p>The error message is confusing me as it appears it has converted the BusinessUser class to an integer. I also tried by putting in the data_class => null and the form then loaded but on submission I got an error that the field was empty though the ID in the hidden field appeared correctly hen the form was generated.</p>
  
  <p>Can someone point me in the right direction as have a feeling its something very simple that im not seeing. </p>
  
  <p>Thanks</p>
  
  <p>UPDATE: I made some changes as realised I should be setting the company in the controller and passing it into the form, and that all seems to work but when the form is submitted I still get null in the database or if i set the field to @Assert\NotBlank() the form wont submit as  field cannot be blank even though when I check the source I can see the Id of the BusinessUser in the hidden field. </p>
  
  <p>*<em>UPDATE - While implemeting Coma's suggestion below I finally realised where I had gone wrong - it was the transformer it was not returning the object *</em> - If anyone else comes onto this question I would recommend Coma's solution below is much better than having to create a hidden each time. If it is a one off the solution above has been updated to return the object and should work fine.</p>