The answer that is marked as right now (14 Oct 2014) is not the right answer at all.
This is the only right solution:
namespace XXX\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @ORM\Table(name="User_User")
* @ORM\Entity(repositoryClass="UserRepository")
* @UniqueEntity(fields="usernameCanonical", errorPath="username", message="fos_user.username.already_used")
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(name="email", column=@ORM\Column(type="string", name="email", length=255, unique=false, nullable=true)),
* @ORM\AttributeOverride(name="emailCanonical", column=@ORM\Column(type="string", name="email_canonical", length=255, unique=false, nullable=true))
* })
*/
class User extends BaseUser
{
...
}
Also you need to override validation groups for your user form:
# app/config/config.yml
...
fos_user:
profile:
form:
validation_groups: [Default] # Here you can also add your own groups if you have extra validation
registration:
form:
validation_groups: [Default] # Here you can also add your own groups if you have extra validation
What did we do? We just overrode validation groups to not match the FOS default validations. Instead your form will be validated only with Default
group. Described above validation UniqueEntity
that doesn't have any group will be matched by Default
group.