Symfony/FOS - pass the user id variable to a form Type

允我心安 提交于 2019-12-23 20:12:45

问题


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 ?


回答1:


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

  1. 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()
        )
    );
    
  2. Don't inject the User into the type's constructor, just let the options handle it

    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;
    
         /**
         * @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';
        }
    }
    
  3. 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())));
        }
    }
    
  4. 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');
    



回答2:


Your current code passes the array to the create() method, not your CategoryType construct.

$form = $this->get('form.factory')->create(new CategoryType(), array( 'author' => $userId));

should be

$form = $this->get('form.factory')->create(new CategoryType(array( 'author' => $userId)));


来源:https://stackoverflow.com/questions/19813503/symfony-fos-pass-the-user-id-variable-to-a-form-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!