【发布时间】:2017-05-11 19:19:03
【问题描述】:
当我研究 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\Component\Form\FormView"。
【问题讨论】:
标签: symfony fosuserbundle