I've searched and not found anyone with this problem.
I've created my own Data Transformer as set out in the Cookbook and it all seems right but i get the error:
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.
The transformer is below:
<?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->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->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->om
->getRepository('NicheSecurityBundle:BusinessUser')
->findOneById($id);
if (null === $businessUser) {
throw new TransformationFailedException(sprintf(
'An issue with number "%s" does not exist!',
$number
));
}
return $businessUser
}
}
and my form code is
<?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->add('title', 'text');
$builder->add('jobDescription', 'textarea', array(
"label" => "Job Description", )
);
$builder->add('companyDescription', 'textarea', array(
"label" => "Company Description", )
);
$builder->add('salary', 'text');
$builder->add('category', 'entity', array(
'class' => 'NicheJobBundle:Category',
'property' => 'name',
));
$builder->add('responsibilities', 'textarea');
$builder->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->add(
$builder->create('company', 'hidden', array(
'data' => $businessUser,
))->addViewTransformer($transformer)
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Niche\JobBundle\Entity\Job'
));
$resolver->setRequired(array(
'em', 'businessUser'
));
$resolver->setAllowedTypes(array(
'em' => 'Doctrine\Common\Persistence\ObjectManager',
));
}
public function getName()
{
return 'niche_jobbundle_jobtype';
}
}
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.
Can someone point me in the right direction as have a feeling its something very simple that im not seeing.
Thanks
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.
*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 * - 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.