Symfony 不验证实体
Symfony does not validate entity
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'
这可以通过向您的用户 class 添加 UniqueConstraint
来防止,如下所示:
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @UniqueEntity(fields={"email"}, message="This email address is already in use")
* @UniqueEntity(fields={"username"}, message="This username is already in use")
*/
class User extends BaseUser
{
//...
}
事实证明,我在使用相同版本构建的应用程序上也遇到了这种情况,所以感谢您发现这一点,但从未想过要对其进行测试:-)
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'
这可以通过向您的用户 class 添加 UniqueConstraint
来防止,如下所示:
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @UniqueEntity(fields={"email"}, message="This email address is already in use")
* @UniqueEntity(fields={"username"}, message="This username is already in use")
*/
class User extends BaseUser
{
//...
}
事实证明,我在使用相同版本构建的应用程序上也遇到了这种情况,所以感谢您发现这一点,但从未想过要对其进行测试:-)