我正试图从FOSUserBundle注册表中删除用户名字段,如本答案中的步骤2所述。
这是我创建的FormType类,用于覆盖默认值:
<?php
// src/UserBundle/Form/Type/RegistrationFormType.php
namespace UserBundle'Form'Type;
use FOS'UserBundle'Form'Type'RegistrationFormType as BaseFormType;
class RegistrationFormType extends BaseFormType
{
public function buildForm(FormBuilder $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->remove('username');
}
}
但是,用户名字段仍然显示在表单中。我错过了什么?
您没有覆盖捆绑包配置中的表单服务。
这就是FOSUserBundle不使用您的表单类型的原因。
# register your form-type as a service ...
services:
my_custom_user_registration_form:
class: "UserBundle'Form'Type'RegistrationFormType"
# ... then tell FOSUserBundle to use this form-type service instead of the default
fos_user:
...
registration:
form:
type: my_custom_user_registration_form