我的ExampleUserBundle
扩展FOSUserBundle
。我RegistrationFormType
像这样覆盖:
<?php
// Example/UserBundle/Form/Type/RegistrationFormType.php
namespace Example\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
class RegistrationFormType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder->remove('username');
}
public function getName() {
return 'example_user_registration';
}
}
服务声明:
// Example/UserBundle/Ressources/Config/services.yml
services:
example_user.registration.form.type:
class: Example\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: example_user_registration }
在 /app/config/config.yml 中:
fos_user:
db_driver: orm
firewall_name: main
user_class: Example\UserBundle\Entity\User
registration:
form:
type: example_user_registration
当我使用此表单创建新User
表单时,基本验证规则FOSUserBundle
不会覆盖。(电子邮件 -> 唯一;密码 -> 超过 3 个字符;...)。