错误:无法使用Symfony \ Component \ HttpFoundation \ Request类型的对象作为数组升级到symfony 3.0

时间:2016-04-02 17:29:58

标签: php symfony

我的注册功能有问题,因为我已经升级到Symfony 3.0。

  

错误:无法使用Symfony \ Component \ HttpFoundation \ Request类型的对象作为数组

这是我的功能:

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(),
    ));
}

此错误来自此文件:第92行的vendor \ symfony \ symfony \ src \ Symfony \ Component \ Form \ Extension \ Csrf \ EventListener \ CsrfValidationListener.php

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);
        }
    }
}

我没有找到任何搜索此错误的内容,除了相同的错误,但第67行已经解决了。

1 个答案:

答案 0 :(得分:8)

使用

$form->handleRequest($request);

代替

$form->submit($request);

$form->submit($request->request->all());