I try to set the value of a field "author" from a CategoryType form. I want it to be the user id from the current user logged in with FOS bundle.
my CategoryType form :
namespace My\CategoryBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CategoryType extends AbstractType
{
private $userId;
public function __construct(array $userId)
{
$this->userId = $userId;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('author')
->add('content')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'My\CategoryBundle\Entity\Category',
'auteur' => $this->userId
));
}
/**
* @return string
*/
public function getName()
{
return 'my_categorybundle_category';
}
}
And my controller Action :
public function addAction()
{
$category = new Category;
$user = $this->get('security.context')->getToken()->getUser();
$userId = $user->getId();
$form = $this->get('form.factory')->create(new CategoryType(), array( 'author' => $userId));
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($category);
$em->flush();
return $this->redirect($this->generateUrl('mycategory_voir',
array('id' => $category->getId())));
}
}
return $this->render('MyCategoryBundle:Category:add.html.twig',
array(
'form' => $form->createView(),
));
}
I catch this error while running the action :
Catchable Fatal Error: Argument 1 passed to My\CategoryBundle\Form\CategoryType::__construct() must be an array, none given, called in /My/CategoryBundle/Controller/CategoryController.php on line 55 and defined in /My/CategoryBundle/Form/CategoryType.php line 13
Isn't it already an array that I am passing to the form ?
Your problem is on this line
You're not satisfying the contract for
My\CategoryBundle\FormCategoryType::__construct()
. Here, let's look at it another way.The array that you send as the 2nd argument to
Symfony\Component\Form\FormFactory::create()
is what is ultimately injected as$options
arrayMy\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)and
Don't inject the
User
into the type's constructor, just let the options handle itNot even bother putting the author in the form and let the controller handle it
and
Turn your form type into a service and user the DI Container to inject the security context
app/config/config.yml
Update your type to receive the security context
Then in your controller, create the form with its service name
Your current code passes the array to the
create()
method, not yourCategoryType
construct.should be