title: Symfony2: Object of class DateTime could not be converted to string
body: <p>I got this error when updating a product: (updating a category is ok)</p>
  
  <blockquote>
    <p>Catchable Fatal Error: Object of class DateTime could not be converted
    to string in
    C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php
    line 188</p>
  </blockquote>
  
  <p><img src="https://i.stack.imgur.com/PJJDb.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/eNgr7.png" alt="enter image description here"></p>
  
  <p><strong>Full stack trace:</strong></p>
  
  <pre><code>Stack Trace
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php at line 188   -
              $this-&gt;loadCatalogue($locale);
          }
          return strtr($this-&gt;catalogues[$locale]-&gt;get((string) $id, $domain), $parameters);
      }
      /**
  at ErrorHandler -&gt;handle ('4096', 'Object of class DateTime could not be converted to string', 'C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php', '188', array('id' =&gt; 'This value should be blank.', 'parameters' =&gt; array('{{ value }}' =&gt; object(DateTime)), 'domain' =&gt; 'validators', 'locale' =&gt; 'en')) 
  at strtr ('This value should be blank.', array('{{ value }}' =&gt; object(DateTime))) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php at line 188   + 
  at Translator -&gt;trans ('This value should be blank.', array('{{ value }}' =&gt; object(DateTime)), 'validators') 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 93   + 
  at ExecutionContext -&gt;addViolation ('This value should be blank.', array('{{ value }}' =&gt; object(DateTime))) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\Constraints\BlankValidator.php at line 30   + 
  at BlankValidator -&gt;validate (object(DateTime), object(Blank)) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 276   + 
  at ExecutionContext -&gt;executeConstraintValidators (object(DateTime), array(object(Blank))) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 241   + 
  at ExecutionContext -&gt;validateValue (object(DateTime), array(object(Blank))) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ValidationVisitor.php at line 110   + 
  at ValidationVisitor -&gt;visit (object(PropertyMetadata), object(DateTime), 'Default', 'data.created_at') 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\Mapping\MemberMetadata.php at line 47   + 
  at MemberMetadata -&gt;accept (object(ValidationVisitor), object(DateTime), 'Default', 'data.created_at', null) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\Mapping\ClassMetadata.php at line 114   + 
  at ClassMetadata -&gt;accept (object(ValidationVisitor), object(Product), 'Default', 'data') 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ValidationVisitor.php at line 162   + 
  at ValidationVisitor -&gt;validate (object(Product), 'Default', 'data', true, false) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 227   + 
  at ExecutionContext -&gt;validate (object(Product), 'data', 'Default', true) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Validator\Constraints\FormValidator.php at line 60   + 
  at FormValidator -&gt;validate (object(Form), object(Form)) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 276   + 
  at ExecutionContext -&gt;executeConstraintValidators (object(Form), array(object(Form))) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ExecutionContext.php at line 241   + 
  at ExecutionContext -&gt;validateValue (object(Form), array(object(Form))) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ValidationVisitor.php at line 110   + 
  at ValidationVisitor -&gt;visit (object(ClassMetadata), object(Form), 'Default', '') 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\Mapping\ClassMetadata.php at line 107   + 
  at ClassMetadata -&gt;accept (object(ValidationVisitor), object(Form), 'Default', '') 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\ValidationVisitor.php at line 162   + 
  at ValidationVisitor -&gt;validate (object(Form), 'Default', '', false, false) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Validator\Validator.php at line 90   + 
  at Validator -&gt;validate (object(Form)) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener.php at line 55   + 
  at ValidationListener -&gt;validateForm (object(FormEvent)) 
  at call_user_func (array(object(ValidationListener), 'validateForm'), object(FormEvent)) 
  in C:\xampp\htdocs\PhpProject1\app\cache\dev\classes.php at line 1655   + 
  at EventDispatcher -&gt;doDispatch (array(array(object(ValidationListener), 'validateForm')), 'form.post_bind', object(FormEvent)) 
  in C:\xampp\htdocs\PhpProject1\app\cache\dev\classes.php at line 1588   + 
  at EventDispatcher -&gt;dispatch ('form.post_bind', object(FormEvent)) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\ImmutableEventDispatcher.php at line 42   + 
  at ImmutableEventDispatcher -&gt;dispatch ('form.post_bind', object(FormEvent)) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 624   + 
  at Form -&gt;submit (object(Request)) 
  in C:\xampp\htdocs\PhpProject1\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 638   + 
  at Form -&gt;bind (object(Request)) 
  in C:\xampp\htdocs\PhpProject1\src\Mobile\BusinessBundle\Controller\ProductController.php at line 132   + 
  at ProductController -&gt;updateAction (object(Request), '7') 
  at call_user_func_array (array(object(ProductController), 'updateAction'), array(object(Request), '7')) 
  in C:\xampp\htdocs\PhpProject1\app\bootstrap.php.cache at line 2770   + 
  at HttpKernel -&gt;handleRaw (object(Request), '1') 
  in C:\xampp\htdocs\PhpProject1\app\bootstrap.php.cache at line 2744   + 
  at HttpKernel -&gt;handle (object(Request), '1', true) 
  in C:\xampp\htdocs\PhpProject1\app\bootstrap.php.cache at line 2874   + 
  at ContainerAwareHttpKernel -&gt;handle (object(Request), '1', true) 
  in C:\xampp\htdocs\PhpProject1\app\bootstrap.php.cache at line 2175   + 
  at Kernel -&gt;handle (object(Request)) 
  in C:\xampp\htdocs\PhpProject1\web\app_dev.php at line 29   + 
  </code></pre>
  
  <p>Here is the code:
  <strong>Product.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Mobile\BusinessBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Lib\Functions;
  
  /**
  * Product
  */
  class Product
  {
  
  /**
   * @var integer
   */
  private $id;
  
  /**
   * @var string
   */
  private $name;
  
  /**
   * @var string
   */
  private $slug;
  
  /**
   * @var float
   */
  private $price;
  
  /**
   * @var float
   */
  private $discount;
  
  /**
   * @var string
   */
  private $description;
  
  /**
   * @var string
   */
  private $detail;
  
  /**
   * @var string
   */
  private $sku;
  
  /**
   * @var integer
   */
  private $views;
  
  /**
   * @var boolean
   */
  private $is_active;
  
  /**
   * @var \DateTime
   */
  private $created_at;
  
  /**
   * @var \DateTime
   */
  private $updated_at;
  
  /**
   * @var \Doctrine\Common\Collections\Collection
   */
  private $categories;
  
  /**
   * Constructor
   */
  public function __construct()
  {
      $this-&gt;categories = new \Doctrine\Common\Collections\ArrayCollection();
  }
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set name
   *
   * @param string $name
   * @return Product
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  
      return $this;
  }
  
  /**
   * Get name
   *
   * @return string 
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Set slug
   *
   * @param string $slug
   * @return Product
   */
  public function setSlug($slug)
  {
      $this-&gt;slug = $slug;
  
      return $this;
  }
  
  /**
   * Get slug
   *
   * @return string 
   */
  public function getSlug()
  {
      return $this-&gt;slug;
  }
  
  /**
   * Set price
   *
   * @param float $price
   * @return Product
   */
  public function setPrice($price)
  {
      $this-&gt;price = $price;
  
      return $this;
  }
  
  /**
   * Get price
   *
   * @return float 
   */
  public function getPrice()
  {
      return $this-&gt;price;
  }
  
  /**
   * Set discount
   *
   * @param float $discount
   * @return Product
   */
  public function setDiscount($discount)
  {
      $this-&gt;discount = $discount;
  
      return $this;
  }
  
  /**
   * Get discount
   *
   * @return float 
   */
  public function getDiscount()
  {
      return $this-&gt;discount;
  }
  
  /**
   * Set description
   *
   * @param string $description
   * @return Product
   */
  public function setDescription($description)
  {
      $this-&gt;description = $description;
  
      return $this;
  }
  
  /**
   * Get description
   *
   * @return string 
   */
  public function getDescription()
  {
      return $this-&gt;description;
  }
  
  /**
   * Set detail
   *
   * @param string $detail
   * @return Product
   */
  public function setDetail($detail)
  {
      $this-&gt;detail = $detail;
  
      return $this;
  }
  
  /**
   * Get detail
   *
   * @return string 
   */
  public function getDetail()
  {
      return $this-&gt;detail;
  }
  
  /**
   * Set sku
   *
   * @param string $sku
   * @return Product
   */
  public function setSku($sku)
  {
      $this-&gt;sku = $sku;
  
      return $this;
  }
  
  /**
   * Get sku
   *
   * @return string 
   */
  public function getSku()
  {
      return $this-&gt;sku;
  }
  
  /**
   * Set views
   *
   * @param integer $views
   * @return Product
   */
  public function setViews($views)
  {
      $this-&gt;views = $views;
  
      return $this;
  }
  
  /**
   * Get views
   *
   * @return integer 
   */
  public function getViews()
  {
      return $this-&gt;views;
  }
  
  /**
   * Set is_active
   *
   * @param boolean $isActive
   * @return Product
   */
  public function setIsActive($isActive)
  {
      $this-&gt;is_active = $isActive;
  
      return $this;
  }
  
  /**
   * Get is_active
   *
   * @return boolean 
   */
  public function getIsActive()
  {
      return $this-&gt;is_active;
  }
  
  /**
   * Set created_at
   *
   * @param \DateTime $createdAt
   * @return Product
   */
  public function setCreatedAt($createdAt)
  {
      $this-&gt;created_at = $createdAt;
  
      return $this;
  }
  
  /**
   * Get created_at
   *
   * @return \DateTime 
   */
  public function getCreatedAt()
  {
      return $this-&gt;created_at;
  }
  
  /**
   * Set updated_at
   *
   * @param \DateTime $updatedAt
   * @return Product
   */
  public function setUpdatedAt($updatedAt)
  {
      $this-&gt;updated_at = $updatedAt;
  
      return $this;
  }
  
  /**
   * Get updated_at
   *
   * @return \DateTime 
   */
  public function getUpdatedAt()
  {
      return $this-&gt;updated_at;
  }
  
  /**
   * Add categories
   *
   * @param \Mobile\BusinessBundle\Entity\Category $categories
   * @return Product
   */
  public function addCategorie(\Mobile\BusinessBundle\Entity\Category $categories)
  {
      $this-&gt;categories[] = $categories;
  
      return $this;
  }
  
  /**
   * Remove categories
   *
   * @param \Mobile\BusinessBundle\Entity\Category $categories
   */
  public function removeCategorie(\Mobile\BusinessBundle\Entity\Category $categories)
  {
      $this-&gt;categories-&gt;removeElement($categories);
  }
  
  /**
   * Get categories
   *
   * @return \Doctrine\Common\Collections\Collection 
   */
  public function getCategories()
  {
      return $this-&gt;categories;
  }
  /**
   * @ORM\PrePersist
   */
  public function onPrePersist()
  {
      $this-&gt;created_at = new \DateTime();
      $this-&gt;updated_at = new \DateTime();
      $this-&gt;views = 0;
      $this-&gt;is_active = true;
      $this-&gt;slug = Functions::url_friendly($this-&gt;name);
  }
  
  /**
   * @ORM\PreUpdate
   */
  public function onPreUpdate()
  {
      $this-&gt;updated_at = new \DateTime();
  }
  
  public function __toString()
  {
      return $this-&gt;name;
  }
  }
  </code></pre>
  
  <p><strong>Product.orm.yml</strong></p>
  
  <pre><code>Mobile\BusinessBundle\Entity\Product:
  type: entity
  repositoryClass: Mobile\BusinessBundle\Entity\ProductRepository
  manyToMany:
      categories:
          targetEntity: Category
          inversedBy: products
  table: products
  fields:
      id:
          type: bigint
          id: true
          generator:
              strategy: AUTO
      name:
          type: string
          length: 255
      slug:
          type: string
          length: 255
      price:
          type: decimal
          length: null
      discount:
          type: decimal
          length: null
      description:
          type: text
          length: null
      detail:
          type: text
          length: null
      sku:
          type: string
          length: 45
      views:
          type: bigint
      is_active:
          type: boolean
      created_at:
          type: datetime
      updated_at:
          type: datetime
  lifecycleCallbacks:
    prePersist: [onPrePersist]
    preUpdate: [onPreUpdate]
  </code></pre>
  
  <p><strong>ProductType.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Mobile\BusinessBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class ProductType extends AbstractType
  {
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('name')
          -&gt;add('slug')
          -&gt;add('price')
          -&gt;add('discount')
          -&gt;add('description')
          -&gt;add('detail')
          -&gt;add('sku')
          -&gt;add('views')
          -&gt;add('is_active')
          -&gt;add('created_at')
          -&gt;add('updated_at')
          -&gt;add('categories')
      ;
  }
  
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'Mobile\BusinessBundle\Entity\Product'
      ));
  }
  
  public function getName()
  {
      return 'mobile_businessbundle_producttype';
  }
  }
  </code></pre>
  
  <p><strong>ProductController.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Mobile\BusinessBundle\Controller;
  
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  
  use Mobile\BusinessBundle\Entity\Product;
  use Mobile\BusinessBundle\Form\ProductType;
  
  /**
   * Product controller.
   *
   */
  class ProductController extends Controller
  {
  
  /**
   * Lists all Product entities.
   *
   */
  public function indexAction()
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $entities = $em-&gt;getRepository('MobileBusinessBundle:Product')-&gt;findAll();
  
      return $this-&gt;render('MobileBusinessBundle:Product:index.html.twig', array(
          'entities' =&gt; $entities,
      ));
  }
  /**
   * Creates a new Product entity.
   *
   */
  public function createAction(Request $request)
  {
      $entity  = new Product();
      $form = $this-&gt;createForm(new ProductType(), $entity);
      $form-&gt;bind($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($entity);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('product_show', array('id' =&gt; $entity-&gt;getId())));
      }
  
      return $this-&gt;render('MobileBusinessBundle:Product:new.html.twig', array(
          'entity' =&gt; $entity,
          'form'   =&gt; $form-&gt;createView(),
      ));
  }
  
  /**
   * Displays a form to create a new Product entity.
   *
   */
  public function newAction()
  {
      $entity = new Product();
      $form   = $this-&gt;createForm(new ProductType(), $entity);
  
      return $this-&gt;render('MobileBusinessBundle:Product:new.html.twig', array(
          'entity' =&gt; $entity,
          'form'   =&gt; $form-&gt;createView(),
      ));
  }
  
  /**
   * Finds and displays a Product entity.
   *
   */
  public function showAction($id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $entity = $em-&gt;getRepository('MobileBusinessBundle:Product')-&gt;find($id);
  
      if (!$entity) {
          throw $this-&gt;createNotFoundException('Unable to find Product entity.');
      }
  
      $deleteForm = $this-&gt;createDeleteForm($id);
  
      return $this-&gt;render('MobileBusinessBundle:Product:show.html.twig', array(
          'entity'      =&gt; $entity,
          'delete_form' =&gt; $deleteForm-&gt;createView(),        ));
  }
  
  /**
   * Displays a form to edit an existing Product entity.
   *
   */
  public function editAction($id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $entity = $em-&gt;getRepository('MobileBusinessBundle:Product')-&gt;find($id);
  
      if (!$entity) {
          throw $this-&gt;createNotFoundException('Unable to find Product entity.');
      }
  
      $editForm = $this-&gt;createForm(new ProductType(), $entity);
      $deleteForm = $this-&gt;createDeleteForm($id);
  
      return $this-&gt;render('MobileBusinessBundle:Product:edit.html.twig', array(
          'entity'      =&gt; $entity,
          'edit_form'   =&gt; $editForm-&gt;createView(),
          'delete_form' =&gt; $deleteForm-&gt;createView(),
      ));
  }
  
  /**
   * Edits an existing Product entity.
   *
   */
  public function updateAction(Request $request, $id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $entity = $em-&gt;getRepository('MobileBusinessBundle:Product')-&gt;find($id);
  
      if (!$entity) {
          throw $this-&gt;createNotFoundException('Unable to find Product entity.');
      }
  
      $deleteForm = $this-&gt;createDeleteForm($id);
      $editForm = $this-&gt;createForm(new ProductType(), $entity);
      $editForm-&gt;bind($request);
  
      if ($editForm-&gt;isValid()) {
          $em-&gt;persist($entity);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('product_edit', array('id' =&gt; $id)));
      }
  
      return $this-&gt;render('MobileBusinessBundle:Product:edit.html.twig', array(
          'entity'      =&gt; $entity,
          'edit_form'   =&gt; $editForm-&gt;createView(),
          'delete_form' =&gt; $deleteForm-&gt;createView(),
      ));
  }
  /**
   * Deletes a Product entity.
   *
   */
  public function deleteAction(Request $request, $id)
  {
      $form = $this-&gt;createDeleteForm($id);
      $form-&gt;bind($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $entity = $em-&gt;getRepository('MobileBusinessBundle:Product')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Product entity.');
          }
  
          $em-&gt;remove($entity);
          $em-&gt;flush();
      }
  
      return $this-&gt;redirect($this-&gt;generateUrl('product'));
  }
  
  /**
   * Creates a form to delete a Product entity by id.
   *
   * @param mixed $id The entity id
   *
   * @return \Symfony\Component\Form\Form The form
   */
  private function createDeleteForm($id)
  {
      return $this-&gt;createFormBuilder(array('id' =&gt; $id))
          -&gt;add('id', 'hidden')
          -&gt;getForm()
      ;
  }
  }
  </code></pre>
  
  <p><strong>edit.html.twig</strong></p>
  
  <pre><code>{% extends '::base.html.twig' %}
  
  {% block body -%}
      &lt;h1&gt;Product edit&lt;/h1&gt;
  
      &lt;form action="{{ path('product_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}&gt;
          &lt;input type="hidden" name="_method" value="PUT" /&gt;
          {{ form_widget(edit_form) }}
          &lt;p&gt;
              &lt;button type="submit"&gt;Edit&lt;/button&gt;
          &lt;/p&gt;
      &lt;/form&gt;
  
          &lt;ul class="record_actions"&gt;
      &lt;li&gt;
          &lt;a href="{{ path('product') }}"&gt;
              Back to the list
          &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;form action="{{ path('product_delete', { 'id': entity.id }) }}" method="post"&gt;
              &lt;input type="hidden" name="_method" value="DELETE" /&gt;
              {{ form_widget(delete_form) }}
              &lt;button type="submit"&gt;Delete&lt;/button&gt;
          &lt;/form&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  {% endblock %}
  </code></pre>
  
  <p><strong>Updated:</strong>
  The error occurred because I created the file validation.yml for the Product Entity with created_at : Blank.
      Mobile\BusinessBundle\Entity\Product:
          properties:
              created_at:
                  - Blank: ~</p>