title: How to set a default value in a Symfony 2 form field?
body: <p>I've been trying to set up a form with <code>Symfony 2</code>. </p>
  
  <p>So I followed <a href="http://symfony.com/doc/current/book/forms.html" rel="nofollow">the tutorial</a> and I've created a special class for creating the form and handling the validation process outside the controller (as shown in the documentation)</p>
  
  <p>But now I need to fill in a field automatically, I've heard that I have to do it in the ProductType.php, where the form (for my product) is created.</p>
  
  <p>But I don't know how to do, here is my buildForm function in ProductType.php  :</p>
  
  <pre><code>class QuotesType extends AbstractType
  {
      private $id;
  
      public function __construct($id){
          $this-&gt;product_id = $id;
      }
  
      public function buildForm(FormBuilder $builder, array $options)
      {
          $builder
              -&gt;add('user_name',              'text')
              -&gt;add('user_lastname',          'text')
              -&gt;add('user_email',             'email')
              -&gt;add('user_comments',          'textarea')
              -&gt;add('user_product_id',        'hidden', array(
                  'data' =&gt; $this-&gt;product_id,
              ));
          ;
      }
  </code></pre>
  
  <p>and it obviously doesnt work since I got a SQL error saying that my field is null.</p>
  
  <p>How can I put a default value to the user_product_id ? should I do it directly to the object ?</p>
  
  <p>EDIT:</p>
  
  <p>Here is a part of the code of my entity :</p>
  
  <pre><code>namespace QN\MainBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  
  /**
   * QN\MainBundle\Entity\Quotes
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="QN\MainBundle\Entity\QuotesRepository")
   */
  class Quotes
  {
  
      public function __construct($p_id)
      {
          $this-&gt;date = new \Datetime('today');
      }
      /**
       * @var integer $id
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var integer $user_product_id
       *
       * @ORM\Column(name="user_product_id", type="integer")
       */
      private $user_product_id = "1";
  
      /**
       * @var datetime $date
       *
       * @ORM\Column(name="date", type="datetime")
       */
      private $date;
  </code></pre>
  
  <p>And my controller :</p>
  
  <pre><code>public function requestAction($id)
      {
          $repository = $this-&gt;getDoctrine()
                             -&gt;getEntityManager()
                             -&gt;getRepository('QNMainBundle:Categories');
      $categories = $repository-&gt;findAll();
  
      $quote = new Quotes($id);
      $form = $this-&gt;createForm(new QuotesType(), $quote);
  
      $formHandler = new QuotesHandler($form, $this-&gt;get('request'), $this-&gt;getDoctrine()-&gt;getEntityManager());
  
      if( $formHandler-&gt;process() )
      {
          return $this-&gt;redirect( $this-&gt;generateUrl('QNMain_Product', array('id' =&gt; $id)) );
      }
  
      return $this-&gt;render('QNMainBundle:Main:requestaform.html.twig', array(
          'categories' =&gt; $categories,
          'id' =&gt; $id,
          'form' =&gt; $form-&gt;createView(),
      ));
  
  }
  </code></pre>
  
  <p>My Handler :</p>
  
  <pre><code>namespace QN\MainBundle\Form;
  
  use Symfony\Component\Form\Form;
  use Symfony\Component\HttpFoundation\Request;
  use Doctrine\ORM\EntityManager;
  use QN\MainBundle\Entity\Quotes;
  
  class QuotesHandler
  {
      protected $form;
      protected $request;
      protected $em;
  
      public function __construct(Form $form, Request $request, EntityManager $em)
      {
          $this-&gt;form    = $form;
          $this-&gt;request = $request;
          $this-&gt;em      = $em;
      }
  
      public function process()
      {
          if( $this-&gt;request-&gt;getMethod() == 'POST' )
          {
              $this-&gt;form-&gt;bindRequest($this-&gt;request);
  
              if( $this-&gt;form-&gt;isValid() )
              {
                  $this-&gt;onSuccess($this-&gt;form-&gt;getData());
  
                  return true;
              }
          }
  
          return false;
      }
  
      public function onSuccess(Quotes $quote)
      {
          $this-&gt;em-&gt;persist($quote);
          $this-&gt;em-&gt;flush();
      }
  
  }
  </code></pre>
  
  <p>I've also put here the <code>Date</code> I try to set up in the entity, I might do something wrong in both case since I can't make it work neither ..Date is not in the <code>buildForm</code> function, I don't know if I should ..</p>