【发布时间】:2017-01-10 10:52:50
【问题描述】:
我已经在我的 Symfony 项目中安装了 FOSUserBundle。现在我想删除 FOSUserBundle 默认提供的注册表单字段。
注册表单字段是:
用户名
电子邮件 ID
密码
重复密码
现在我不想在用户注册时使用 Email 字段,因此我覆盖了我的捆绑包中的注册表单。
\\ Front\FrontBundle\Form\RegistrationType.php
<?php
namespace Front\FrontBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->remove('email'); // here I code for remove email field.
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
// Or for Symfony < 2.8
// return 'fos_user_registration';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
// For Symfony 2.x
public function getName()
{
return $this->getBlockPrefix();
}
}
然后我更改 config.yml 和 services.yml 文件
\\ App/config/config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: Front\FrontBundle\Entity\User
registration:
form:
type: Front\FrontBundle\Form\RegistrationType
\\app/config/services.yml
services:
app.form.registration:
class: Front\FrontBundle\Form\RegistrationType
tags:
- { name: form.type, alias: app_user_registration }
所以在完成此电子邮件字段后,从我的注册表中删除,但是当我在填写 username、password、repeat password 后提交表单时,它会给我任何 The email is not valid 的错误。
所以我需要更改任何其他文件以删除带有电子邮件字段的电子邮件验证?
谢谢。
【问题讨论】:
-
email是 FOSUser 提供给您扩展的模型的必填字段。如果你不想要它,就不要扩展 FOS\UserBundle\Model\User。 -
@Federico 你能详细解释一下吗?我需要更改哪个文件?
-
由于
email是在 FOSUserBundle 提供的抽象中定义的,因此您几乎不得不使用它。我相信简单的解决方案是覆盖模型的 setUsername 方法,并在此处设置一个假的(有效且唯一的)电子邮件。 -
但是如果您不需要 FOSUserBundle 提供的大部分功能,就不要使用它。
标签: symfony fosuserbundle