好的,我有它的工作。无论我是否希望在所有用户表单上“验证”电子邮件唯一性,我一直遇到的问题是我能够通过验证组解决的问题。User
这是我实体的相关部分。注意注释groups={"registration"}
部分:@UniqueEntity
/**
* VNN\PressboxBundle\Entity\User
*
* @ORM\Table(name="user")
* @ORM\Entity
* @UniqueEntity(fields="email", message="Sorry, this email address is already in use.", groups={"registration"})
* @UniqueEntity(fields="username", message="Sorry, this username is already taken.", groups={"registration"})
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $email
* @Assert\NotBlank()
* @Assert\Email()
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
*/
private $email;
/**
* @var string $username
* @Assert\NotBlank()
*
* @ORM\Column(name="username", type="string", length=255, unique=true)
*/
private $username;
还有我的表单类(注意最后一个函数):
<?php
namespace VNN\PressboxBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('first_name')
->add('last_name')
->add('email')
->add('username')
->add('password', 'password')
->add('athletic_director_email')
->add('school')
->add('active', 'hidden')
->add('transition_key', 'hidden')
;
}
public function getName()
{
return 'vnn_pressboxbundle_usertype';
}
// The following function is what's important here.
// This tells this form to use the "registration" validation group.
public function getDefaultOptions(array $options)
{
return array(
'validation_groups' => array('registration')
);
}
}
我相信就是这样。我不认为我必须在其他任何地方做任何特别的事情。