我想验证用户名字段,这是一个fosuser字段,但是在我自己的验证规则下。
以下是我的代码段:
FormType - UserType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
...
}
实体 - User.php
class User extends BaseUser
{
//no mention of any of the fosuser properties here
...
}
验证:validation.yml
Project\MyBundle\Entity\User:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: username
errorPath: username
message: 'This username is already in use.'
properties:
dob:
- NotBlank: ~
- Type: \DateTime
Controller:UserController.php
public function createAction(Request $request, $brandId)
{
$entity = new User();
$form = $this->createCreateForm($entity, $brandId);
$form->handleRequest($request);
if ($form->isValid()) {
...
}
}
验证总是失败,即。即使我在表单中输入已存在的用户名,$ form-> isValid()也是如此。请帮助,因为到目前为止我还没有找到解决方案。我尝试将验证文件夹下的orm.xml文件复制到myBundle。但它仍然没有效果。任何帮助将不胜感激。
当保存发生时,我也会遇到重复的插入错误。
An exception occurred while executing 'UPDATE user SET username = ?, username_canonical = ? WHERE id = ?' with params ["sub5", "sub5", 27]:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'sub5' for key 'UNIQ_7BB0CD5292FC23A8'
答案 0 :(得分:0)