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

FosUserBunde Update User Profile WITHOUT the need of current password

当我研究用于编辑用户配置文件的 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),
        ));

然后呈现文件 "current password" 但是我想编辑配置文件而不需要一直输入用户密码。有办法吗?

实际上,我尝试扩展 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');

我收到以下错误:

Neither the property "current_password" nor one of the methods "current_password()", "getcurrent_password()"/"iscurrent_password()"/"hascurrent_password()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".

只是删除 'current_password' 字段?

$builder->remove('current_password');

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

有了这个,你可以尝试在初始化时在表单中插入当前密码。

祝你好运!

正如@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) }} 字段正在使用自定义模板,以防您使用它来呈现每个元素分别.