title: symfony2: How to add validation rules to username?
body: <p>Since I am extending the <code>BaseUser</code>, I don't have the property <code>$name</code> in my user entity. How can I add validation to it? Can I just add a property <code>$username</code> to this entity? Would that mess with or override certain functionality or validation or such that is provided natively to a user class?</p>
  <p>I rad this question: <a href="https://stackoverflow.com/questions/16702353/fosuserbundle-change-username-validation">FOSUserBundle - Validation for username, password or email fields</a>.<br />
  But since I am using a bunch of annotation validations, that won't work for me.</p>
  <p>Follow-up question - I see that in <code>fosUSerBundle</code> XML file, for validating the user that it has natively a checker for use of a taken username. I don't get that functionality, mine goes straight to a SQL error message if I try to add a user with the same name as an existing user. Is this somehow because I am using annotations which overrides the XML validation file all together?</p>
  <pre><code>&lt;?php
  namespace BizTV\UserBundle\Entity;
  
  use BizTV\UserBundle\Validator\Constraints as BizTVAssert;
  use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  
  
  use FOS\UserBundle\Model\User as BaseUser;
  
  use Doctrine\ORM\Mapping as ORM;
  
  use BizTV\BackendBundle\Entity\company as company;
  
  
  class User extends BaseUser implements AdvancedUserInterface
  {
      /**
       * @ORM\Id
       * @ORM\Column(type=&quot;integer&quot;)
       * @ORM\GeneratedValue(strategy=&quot;AUTO&quot;)
       */
      protected $id;  
  
  //TODO: Add regex on $name for a-z, A-Z, 0-9 and _ (underscore) 
  //TODO: Add constraint on $name    * @BizTVAssert\NameExists    (and finish coding this constraint)
  
      /**
      * @var object BizTV\BackendBundle\Entity\company
      *  
      * @ORM\ManyToOne(targetEntity=&quot;BizTV\BackendBundle\Entity\company&quot;)
      * @ORM\JoinColumn(name=&quot;company&quot;, referencedColumnName=&quot;id&quot;, nullable=false)
      */
      protected $company; 
  
      /**
      * @var object BizTV\UserBundle\Entity\UserGroup
      * @ORM\ManyToOne(targetEntity=&quot;BizTV\UserBundle\Entity\UserGroup&quot;)
      * @ORM\JoinColumn(name=&quot;userGroup&quot;, referencedColumnName=&quot;id&quot;, nullable=true)
      */
      protected $userGroup;   
  </code></pre>
  <p>So I have looked here (<a href="https://stackoverflow.com/questions/7322441/symfony2-using-validation-groups-in-form">symfony2 using validation groups in form</a>) and tried that solution by adding this to the controller when building the form. Nothing happens (still jumps straight to the INSERT INTO SQL error)</p>
  <pre><code>    $entity  = new User();
      $request = $this-&gt;getRequest();
      $form    = $this-&gt;createForm(new newUserType($tempCompany), $entity, array('validation_groups'=&gt;'registration'));
      $form-&gt;bind($request);
  </code></pre>
  <p>I have also tried to set this validation group &quot;registration group&quot; (that is supposed to give you unique user &amp; email validation &quot;for free&quot;) this way, in the form class:</p>
  <pre><code>use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
  $resolver-&gt;setDefaults(array(
      'validation_groups' =&gt; array('registration'),
  ));
  }
  </code></pre>
  <p>Still nothing happens. Straight to the SQL error upon save.</p>