FosUserBunde更新用户配置文件,无需当前密码

时间:2017-05-11 19:19:04

标签: symfony fosuserbundle

在我研究FosUserBundle的默认表单生成器以编辑用户配置文件(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_password”和方法“current_password()”,“getcurrent_password()”/“iscurrent_password()”/“hascurrent_password()”或“__call()”都不存在,并且在课堂上具有公共访问权限“的Symfony \元器件\表格\ FormView的”。

2 个答案:

答案 0 :(得分:2)

只需删除'current_password'字段?

$builder->remove('current_password');

如果它不起作用,您可以使用PRE_SET_DATA事件: Symfony doc

使用此功能,您可以尝试在初始化时在表单中插入当前密码。

祝你好运!

答案 1 :(得分:0)

正如@fireaxe所说。 换句话说,把它放到你的buildForm中:

        parent::buildForm($builder, $options);        
        //Add some other fields
        $builder->remove('current_password');

另外请确保 REMOVED 您使用自定义模板中的任何{{ form_row(form.current_password) }}或任何{{ form_widget(form.current_password) }}字段,以防您使用它来单独呈现每个元素。