FOSUserBundle:“无法找到该文件。”重置/更改密码

时间:2017-07-04 14:35:00

标签: php symfony fosuserbundle

使用FOSUserBundle时遇到了一个奇怪的错误。当我尝试为在其实体中具有上传图像文件名的用户重置或更改密码时,我收到错误:“找不到该文件。”如果图像字段为空,一切都很好。所以我只是通过在resetAction中的$ form-> setData($ user)之前将photo设置为null来解决此问题,然后在表单提交和验证之后我再次设置旧的照片值。

我的解决方案:

/**
 * Reset user password.
 *
 * @param Request $request
 * @param string  $token
 *
 * @return Response
 */
public function resetAction(Request $request, $token)
{
    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
    $formFactory = $this->get('fos_user.resetting.form.factory');
    /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
    $userManager = $this->get('fos_user.user_manager');
    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
    $dispatcher = $this->get('event_dispatcher');

    $user = $userManager->findUserByConfirmationToken($token);

    if (null === $user) {
        throw new NotFoundHttpException(sprintf('The user with "confirmation token" does not exist for value "%s"', $token));
    }

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

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

    $form = $formFactory->createForm();

    //fix error upon form validation: "The file could not be found."
    $photo = $user->getPhoto();
    $user->setPhoto(null);

    $form->setData($user);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $event = new FormEvent($form, $request);
        $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS, $event);

        //set photo name instead of null
        $user->setPhoto($photo);

        $userManager->updateUser($user);

        if (null === $response = $event->getResponse()) {
            $url = $this->generateUrl('fos_user_profile_show');
            $response = new RedirectResponse($url);
        }

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

        return $response;
    }

    return $this->render('@FOSUser/Resetting/reset.html.twig', array(
        'token' => $token,
        'form' => $form->createView(),
    ));
}

我的用户实体的片段:

class User extends BaseUser
{
    /**
     * @ var string
     *
     * @ORM\Column(name="photo", type="string", length=255, nullable=true)
     * @Assert\File(mimeTypes={ "image/jpeg", "image/png" })
     */
    private $photo;

我不明白为什么在没有文件上传输入的情况下提交表单时会进行文件验证。当您重置密码时,检查用户的所有字段有什么意义?

1 个答案:

答案 0 :(得分:0)

在表单工作流提交中提供handlerequest以填充所有用户行,为表单字段设置所需的false表单