Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Cannot use object of type Symfony\Component\HttpFoundation\Request as array upgrading to symfony 3.0

Tags:

php

symfony

I have a problem with my register function since I've upgraded to Symfony 3.0.

Error: Cannot use object of type Symfony\Component\HttpFoundation\Request as array

Here's my function :

public function registerAction(Request $request) {
    /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
    $userManager = $this->container->get('fos_user.user_manager');
    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
    $dispatcher = $this->container->get('event_dispatcher');

    $user = new Admin();
    $user->setEnabled(true);

    $event = new GetResponseUserEvent($user, $request);
    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);

    if (null !== $event->getResponse()) {
        return $event->getResponse();
    }

    $form = $this->createForm(RegistrationAdminFormType::class);
    $form->setData($user);

    if ('POST' === $request->getMethod()) {
        $form->submit($request);

        if ($form->isValid()) {

            // Attribution du username
            $username = $this->getDoctrine()->getManager()->getRepository('PACESUserBundle:Admin')->getNewUsername();
            $user->setUsername($username);

            $user->setNom(ucfirst(strtolower($form->get('nom')->getData())));
            $user->setPrenom(ucfirst(strtolower($form->get('prenom')->getData())));

            $event = new FormEvent($form, $request);
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

            $userManager->updateUser($user);
            if (null === $response = $event->getResponse()) {
                $url = $this->container->get('router')->generate('fos_user_registration_confirmed');
                $response = new RedirectResponse($url);
            }

            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

            return $response;
        }
    }

    return $this->render('PACESUserBundle:Registration:admin.html.twig', array(
        'form' => $form->createView(),
    ));
}

This error comes from this file : vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Csrf\EventListener\CsrfValidationListener.php at line 92

public function preSubmit(FormEvent $event)
{
    $form = $event->getForm();

    if ($form->isRoot() && $form->getConfig()->getOption('compound')) {
        $data = $event->getData();

        if (!isset($data[$this->fieldName]) || !$this->tokenManager->isTokenValid(new CsrfToken($this->tokenId, $data[$this->fieldName]))) {
            $errorMessage = $this->errorMessage;

            if (null !== $this->translator) {
                $errorMessage = $this->translator->trans($errorMessage, array(), $this->translationDomain);
            }

            $form->addError(new FormError($errorMessage));
        }

        if (is_array($data)) {
            unset($data[$this->fieldName]);
            $event->setData($data);
        }
    }
}

I didn't find anything searching for this error except the same error but with line 67 which was already solved.

like image 420
Kristen Joseph-Delaffon Avatar asked Apr 02 '16 17:04

Kristen Joseph-Delaffon


1 Answers

Use

$form->handleRequest($request);

instead

$form->submit($request);

Or

$form->submit($request->request->all());
like image 134
Arthur Avatar answered Sep 22 '22 13:09

Arthur