Symfony2.1 - The option "em" does not exist when using DataTransformer

Mike picture Mike · Jul 23, 2012 · Viewed 8.1k times · Source

I am using this cookbook recipe to add a data transformer in Symfon 2.1, but I am getting the following error, The option "em" does not exist. Known options are: "attr", "block_name",....

Is this still a valid way to send the entity manager over to the form type?

$taskForm = $this->createForm(new TaskType(), $task, array(
    'em' => $this->getDoctrine()->getEntityManager(),
));

Answer

mimazoo picture mimazoo · Nov 7, 2012

To make the first simple (without dependency injection) Transformer's cookbook recipe work you should add "em" as a known option. You can add it in your form's type class (TaskType in cookbook case) using setRequired() method like this:

class TaskType extends AbstractType {
    //...
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {   
        //...other stuff like $resolver->setDefaults(... if you need it

        $resolver->setRequired(array('em'));
    }
}

Adding 'em' with $resolver->setDefaults() would also work, but in this cookbook case entity manager is needed and so using setRequired() seems better.