Your problem is on this line
$form = $this->get('form.factory')->create(new CategoryType(), array( 'author' => $userId));
You're not satisfying the contract for My\CategoryBundle\FormCategoryType::__construct()
. Here, let's look at it another way.
$form = $this->get('form.factory')->create(
new CategoryType(/* You told PHP to expect an array here */)
, array('author' => $userId)
);
The array that you send as the 2nd argument to Symfony\Component\Form\FormFactory::create()
is what is ultimately injected as $options
array My\CategoryBundle\Form\CategoryType::buildForm()
As I see it, you have a few different ways to resolve this
Update the argument signature AND call for
My\CategoryBundle\FormCategoryType::__construct()
to pass/receive the entire user object (not just their id - remember that you're working with Doctrine relationships at this point, not the lower-level foreign keys that they map to)namespace My\CategoryBundle\Form; use My\CategoryBundle\Entity\User; /* Or whatver your User class is */ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class CategoryType extends AbstractType { private $author; public function __construct( User $author ) { $this->author = $author; }
and
$form = $this->get('form.factory')->create( new CategoryType( $this->get('security.context')->getToken()->getUser() ) );
Don't inject the
User
into the type's constructor, just let the options handle itnamespace My\CategoryBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class CategoryType extends AbstractType { private $userId; /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('title') ->add('author', 'hidden', array('data'=>$options['author'])) ->add('content') ; } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'My\CategoryBundle\Entity\Category' )); } /** * @return string */ public function getName() { return 'my_categorybundle_category'; } }
Not even bother putting the author in the form and let the controller handle it
$form = $this->get('form.factory')->create( new CategoryType() , array('author' => $this->get('security.context')->getToken()->getUser() ) );
and
if ($request->getMethod() == 'POST') { $form->bind($request); if ($form->isValid()) { $category->setAuthor( $this->get('security.context')->getToken()->getUser() ); $em = $this->getDoctrine()->getManager(); $em->persist($category); $em->flush(); return $this->redirect($this->generateUrl('mycategory_voir', array('id' => $category->getId()))); } }
Turn your form type into a service and user the DI Container to inject the security context
app/config/config.yml
services: form.type.my_categorybundle_category: class: My\CategoryBundle\Form\CategoryType tags: - {name: form.type, alias: my_categorybundle_category} arguments: ["%security.context%"]
Update your type to receive the security context
namespace My\CategoryBundle\Form; use Symfony\Component\Security\Core\SecurityContext; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class CategoryType extends AbstractType { private $author; public function __construct( SecurityContext $security ) { $this->author = $security->getToken()->getUser(); }
Then in your controller, create the form with its service name
$form = $this->get('form.factory')->create('my_categorybundle_category');