Symfony不验证实体

时间:2018-06-28 07:19:12

标签: symfony fosuserbundle symfony-validator

Symfony(3.4)不验证实体,不检查是否已使用用户名或电子邮件。我的实体扩展了FOS用户基础实体,因此我没有覆盖任何内容。

这是我的CreateUserType.php

class CreateUserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', EmailType::class)
            ->add('username', TextType::class)
            ->add('plainPassword', RepeatedType::class, [
                'type' => PasswordType::class
            ])
            ->add('enabled', CheckboxType::class)
        ;

        parent::buildForm($builder, $options);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => User::class,
            'csrf_protection' => false
        ]);
    }

    public function getBlockPrefix()
    {
        return "user";
    }
}

还有我的控制器:

$user = new User();

$form = $this->createForm(CreateUserType::class, $user, ['method' => 'POST']);
$form->handleRequest($request);

if($form->isSubmitted() && $form->isValid()) {
    $em->persist($user);
    $em->flush();

    $view = $this->createView(null, [], 201);
    return $this->handleView($view);
}

throw new BadRequestException($form->getErrors());

我收到mysql错误:

Integrity constraint violation: 1062 Duplicate entry 'any_username' for key 'UNIQ_8D93D64992FC23A8'

1 个答案:

答案 0 :(得分:3)

可以通过在用户类中添加<input>来防止这种情况,

[role="input-range"]

结果证明,我在使用相同版本构建的应用程序中也得到了此信息,因此感谢您发现它,尽管从未对其进行测试:-)