FOSUserBundle:"The file could not be found." 错误而 resetting/changing 密码

FOSUserBundle: "The file could not be found." error while resetting/changing password

我在使用 FOSUserBundle 时遇到了一个奇怪的错误。当我尝试为在其实体中具有上传图像文件名的用户重置或更改密码时,我收到错误消息:"The file could not be found." 如果图像字段为空,则一切正常。所以我只是通过在 resetAction 中的 $form->setData($user) 之前将照片设置为 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;

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

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