我正在使用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;
}
}
然后我将它应用到我的User类:
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()
和仅 ,如果我尝试重置密码的帐户已经关联了图片。如果记录没有图像,则不会引起任何问题。
所以这就是我最终失败的地方。我理解:
由于某些原因,Symfony希望在此阶段获得图像
由于其他原因,它没有这样做。
我该如何解决这个问题?在这个阶段我不需要图像,所以如果有办法告诉捆绑可能是理想的。
我对Symfony相对较新,如果对这个问题有明显的答案,我很抱歉,但我无法看到它。
答案 0 :(得分: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
答案 1 :(得分:1)
当您提交表单时,它会调用setLogo
,并将其设置为null
。验证器然后将查找图像,当然不存在(因为它为空)。
快速解决方法是将约束添加到表单类型而不是实体。