我的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
的基本验证规则不会覆盖。 (电子邮件 - &gt;唯一;密码 - &gt;超过3个字符; ...)。
答案 0 :(得分:1)
基本解决方案是为注册表单指定不同的验证组。
解决方案对我有用:Validation of a form - I'm getting the labels two times
答案 1 :(得分:1)
我遇到了类似的问题,但是我想在我的注册表格中添加新的fiesld,我这样工作
我也应该说我在覆盖的注册表格中使用了嵌入表格
class RegistrationType extends AbstractType
{
$builder
->add('avatar',FileManagerType::class,[
"by_reference" => false,
"data_class"=>File::class
])
->add('files', CollectionType::class,[
"entry_type"=> UserFileManagerType::class,
"allow_add" => true,
"allow_delete"=>true,
"by_reference"=>false
])
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
然后在我的嵌入表单中,我这样做
class FileManagerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file',FileType::class,[
"constraints"=>[
new \Symfony\Component\Validator\Constraints\File([
"mimeTypes"=>["image/jpeg", "image/gif", "image/png", "image/jpg"],
"groups"=>["Registration"]
])
]
]);
}
public function configureOptions(OptionsResolver $resolver)
{
//dump($resolver);
$resolver->setDefaults(array(
'data_class' => File::class
));
}
}
然后以其他嵌入形式
class UserFileManagerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file',FileType::class,[
"constraints"=>[
new \Symfony\Component\Validator\Constraints\File([
"mimeTypes"=>["application/pdf", "application/x-pdf"],
"groups"=>["Registration"]
])
]
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => File::class
]);
}
}
现在可以开始工作了
答案 2 :(得分:0)
您可以通过覆盖User属性本身来覆盖FosUserBundle的验证规则。
示例:如果我不再需要用户名,那么我将不得不覆盖 AppBundle \ Entity \ User 中的用户名(扩展了FosUserBundle用户)。
>use Doctrine\ORM\Mapping\AttributeOverrides;
use Doctrine\ORM\Mapping\AttributeOverride;
/**
* User
*
* @ORM\Table(name="fos_user")
* @AttributeOverrides({
* @AttributeOverride(name="username",
* column=@ORM\Column(
* name="username",
* type="string",
* length=255,
* unique=false,
* nullable=true
* )
* ),
* @AttributeOverride(name="usernameCanonical",
* column=@ORM\Column(
* name="usernameCanonical",
* type="string",
* length=255,
* unique=false,
* nullable=true
* )
* )
* })
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
//..
此代码将使用户名在数据库中为空(运行php bin/console doctrine:schema:update --force
之后)。并且在验证规则中不是必需的。