【问题标题】:Symfony FOSUserBundle Password Reset - The file could not be foundSymfony FOSUserBundle 密码重置 - 找不到文件
【发布时间】:2017-11-09 10:15:44
【问题描述】:

我正在使用FOSUserBundle

我定义了以下特征:

trait Logo
{
    /**
     * @Assert\Image()
     * @ORM\Column(type="string", nullable=true)
     */
    private $logo;

    /**
     * @return mixed
     */
    public function getLogo()
    {
        return $this->logo;
    }

    /**
     * @param mixed $logo
     */
    public function setLogo($logo)
    {
        $this->logo = $logo;
    }
}

然后我将它应用到我的用户类:

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    use Logo;

    ...

这一切都很好,并为能够将图像与我的用户相关联提供了基础。

问题是这会破坏 FOS 密码重置 - 在我提交表单后,我收到以下消息,显示为表单错误:

找不到文件。

Symfony Profiler 进一步详细说明如下:

ConstraintViolation {#1368 
  root: Form {#879 …}
  path: "data.logo"
  value: "553d1f015637bf5d97b9eb66f71e6587.jpeg"
}

这特别是由于 logo 属性上的@Assert\Image() 并且在我尝试为其重置密码的帐户已关联图像时显示。如果记录没有图像,它不会导致任何问题。

所以这就是我最终迷路的地方。我明白了:

  1. 出于某种原因,Symfony 想要在这个阶段获取图像,而

  2. 由于某些其他原因,它没有这样做。

我该如何解决这个问题?在这个阶段我不需要图像,所以如果有办法告诉捆绑包可能是理想的。

我对 Symfony 比较陌生,所以如果这个问题有一个明显的答案,但我看不到它,我深表歉意。

【问题讨论】:

  • 这个错误是在什么阶段出现的?当您加载重置表单或提交表单时?​​span>
  • @KhorneHoly - 在将表单作为表单错误提交后 - 我将更新我的问题以澄清这一点。

标签: php symfony fosuserbundle symfony-3.3


【解决方案1】:

为密码重置as explained in the documentation创建一个验证组。

之后,覆盖 ResettingFormType 并定义您自己的验证组来使用。

这里的目标是图像的断言检查不应该在这个表单中进行,但是由于整个用户对象将作为数据注入到表单本身中,所有断言将在表单被告知之前进行否则,请不要这样做。

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'validation_groups' => array('my_resetting'),
    ));
}

覆盖 FormType 的示例:

# AppBundle\Form\Type\ProfileFormType.php
class ProfileFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        # example -> disable change of username in profile_edit
        $builder
            ->add('username', TextType::class, array(
                'disabled' => true,
                'label' => 'form.username',
                'translation_domain' => 'FOSUserBundle'
            ))
        ;
    }

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\ProfileFormType';
    }

    public function getBlockPrefix()
    {
        return 'app_user_profile';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'validation_groups' => array('my_resetting'),
        ));
    }
}

在 config.yml 中

fos_user:
    profile:
        form:
            type: AppBundle\Form\Type\ProfileFormType

【讨论】:

    【解决方案2】:

    当您提交表单时,它将调用setLogo,并将其设置为null。然后验证器将寻找图像,当然它不存在(因为它是空的)。

    快速解决方法是将约束添加到表单类型而不是实体。

    【讨论】:

      猜你喜欢
      • 2017-12-08
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多