Symfony FosUserBunde不需要当前密码即可更新用户配置文件
当我研究FosUserBundle的默认表单生成器以编辑用户配置文件(Symfony FosUserBunde不需要当前密码即可更新用户配置文件,symfony,fosuserbundle,Symfony,Fosuserbundle,当我研究FosUserBundle的默认表单生成器以编辑用户配置文件(FOS\UserBundle\Form\Type\ProfileFormType)时,我注意到以下代码行: $builder->add('current_password', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\PasswordType'), array( 'label' => 'form
FOS\UserBundle\Form\Type\ProfileFormType
)时,我注意到以下代码行:
$builder->add('current_password', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\PasswordType'), array(
'label' => 'form.current_password',
'translation_domain' => 'FOSUserBundle',
'mapped' => false,
'constraints' => new UserPassword($constraintsOptions),
));
这将呈现文件“当前密码”,但我想编辑一个配置文件,而不需要一直输入用户密码。有办法吗
实际上,我试图扩展FosUserBundle的形式,并成功地添加了一些新字段:
namespace AppUserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class UserProfileFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
//$builder->addViewTransformer($this->purifierTransformer);
$builder->add('name',TextType::class,array('label'=>'profile.first_name','required' => false));
$builder->add('surname',TextType::class,array('label'=>'profile.surnname','required' => false));
$builder->add('email',TextType::class,['required' => false]);
$builder->add('description',TextareaType::class,['required' => false]);
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\ProfileFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
// For Symfony 2.x
public function getName()
{
return $this->getBlockPrefix();
}
}
但是,当我需要更新当前登录用户的用户配置文件时,我无法找到一种方法来摆脱密码验证和字段,而不需要用户一直键入密码
编辑1
我试过了
$builder->remove('current_password');
我得到了以下错误:
属性“current\u password”或方法“current\u password()”、“getcurrent\u password()”/“iscurrent\u password()”/“hascurrent\u password()”或“\u call()”都不存在,并且在类“Symfony\Component\Form\FormView”中具有公共访问权限
只需删除“当前密码”字段
$builder->remove('current_password');
如果不起作用,可以使用PRE_SET_数据事件:
使用此选项,您可以尝试在表单中插入当前密码(当它为init时)
祝你好运 正如@fireaxe所说。
换句话说,将其放入您的buildForm
中:
parent::buildForm($builder, $options);
//Add some other fields
$builder->remove('current_password');
还要确保您删除了任何
{form\u row(form.current\u password)}
或任何{form\u widget(form.current\u password)}
字段,以防您使用自定义模板单独呈现每个元素。我相信即使这样做了,您仍然需要删除“配置文件”验证组中的验证,因为editprofile表单仍然会出错。@Sam Janssens我将如何执行此操作?