title: Validation of fields not present in form but in Entity
body: <p>I have a form for user registration, and only username field is present in the form. And in my form, I wish to allow user input the username only. Nicename would be same as username on registration.  </p>
  
  <p>This form is bind to a User entity, i.e., in my form type class:</p>
  
  <pre><code>public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'Some\Bundle\Entity\User',
      ));
  }
  </code></pre>
  
  <p>entity User, which has a NotBlank constraint set for both username and nicename.</p>
  
  <pre><code>namespace Some\Bundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Constraints;
  
  //...
  
  class User
  {
      //...
  
      /**
       * @var string $username
       *
       * @ORM\Column(name="user_login", type="string", length=60, unique=true)
       * @Constraints\NotBlank()
       */
      private $username;
  
      /**
       * @var string $nicename
       *
       * @ORM\Column(name="user_nicename", type="string", length=64)
       * @Constraints\NotBlank()
       */
      private $nicename;
  
      //...
  </code></pre>
  
  <p>However, if I build a form with only username but not nicename, on validation i.e. <code>$form-&gt;isValid()</code> it fails to validate.</p>
  
  <p>To bypass this, I come up with the following:</p>
  
  <pre><code>namespace Some\Bundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Some\Bundle\Form\Type\RegisterType;
  //...
  
  class UserController extends Controller
  {
     //...
  public function registerAction()
  {
      //...
      $request = $this-&gt;getRequest();
      $form = $this-&gt;createForm(new RegisterType());
  
      if ($request-&gt;getMethod() == 'POST') {
          // force set nicename to username.
          $registerFields = $request-&gt;request-&gt;get('register');
          $registerFields['nicename'] = $registerFields['username'];
          $request-&gt;request-&gt;set('register', $registerFields);
  
          $form-&gt;bind($request);
  
          if ($form-&gt;isValid()) {
              $user = $form-&gt;getData();
              //persist $user, etc...
  </code></pre>
  
  <p>And in form type I add this to my <code>buildForm</code> method:  </p>
  
  <pre><code>$builder-&gt;add('nicename', 'hidden');
  </code></pre>
  
  <p>But I find this very inelegant, leave some burden to the controller (extract from the request object, put in data, and put it back into the request object, ouch!), and user can see the hidden field if he were to inspect the source code of generated HTML.</p>
  
  <p>Is there anyway that can at least any controller using the form type does not need do things like above, while retaining the entity constraints?</p>
  
  <p>I cannot change the table schema which backs up the User entity, and I would like to keep the NotBlank constraint.</p>
  
  <hr/>
  
  <p><b>EDIT:</b> After long hassle, I decided to use Validation groups and it worked.</p>
  
  <pre><code>class User
  {
  //...
  
  /**
   * @var string $username
   *
   * @ORM\Column(name="user_login", type="string", length=60, unique=true)
   * @Constraints\NotBlank(groups={"register", "edit"})
   */
  private $username;
  
  /**
   * @var string $nicename
   *
   * @ORM\Column(name="user_nicename", type="string", length=64)
   * @Constraints\NotBlank(groups={"edit"})
   */
  private $nicename;
  </code></pre>
  
  <p>Form Type:</p>
  
  <pre><code>public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'Some\Bundle\Entity\User',
          'validation_groups' =&gt; array('register', 'Default')
      ));
  }
  </code></pre>
  
  <p>That <code>'Default'</code> is needed or it ignores all other constraints I added in the form type <code>buildForm</code> method... Mind you, its case sensitive: <code>'default'</code> does not work.</p>
  
  <p>Though, I find that it is not enough (and sorry I didn't put it in my original question), because when I persist, I need to do this in my controller:</p>
  
  <pre><code>$user-&gt;setNicename($user-&gt;getUsername());
  </code></pre>
  
  <p>As a bonus, I move this from controller to Form Type level by adding a <a href="http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html#adding-an-event-subscriber-to-a-form-class" rel="noreferrer">Form Event Subscriber</a></p>
  
  <p>In form type <code>buildForm</code> method:</p>
  
  <pre><code>$builder-&gt;addEventSubscriber(new RegisterPostBindListener($builder-&gt;getFormFactory()));
  </code></pre>
  
  <p>And the RegisterPostBindListener class</p>
  
  <pre><code>&lt;?php
  namespace Some\Bundle\Form\EventListener;
  
  use Symfony\Component\Form\Event\DataEvent;
  use Symfony\Component\Form\FormFactoryInterface;
  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  use Symfony\Component\Form\FormEvents;
  
  class RegisterPostBindListener implements EventSubscriberInterface
  {
  
  public function __construct(FormFactoryInterface $factory)
  {
  
  }
  
  public static function getSubscribedEvents()
  {
      return array(FormEvents::POST_BIND =&gt; 'setNames');
  }
  
  public function setNames(DataEvent $event)
  {
      $data = $event-&gt;getData();
  
      $data-&gt;setNicename($data-&gt;getUsername());
  }
  }
  </code></pre>