After a long time paying my attention to this problem I found the right and quick solution. After 6 month making science, I got:
Right all:
$traindate = new Element\DateTime('trainDate');
$traindate->setAttributes(array(
'name' => 'trainDate',
'id' => 'trainDate',
'size' => '30',
'class' => 'datepicker',
));
$traindate->setFormat('d/m/Y'); //ONLY WORKS ON THIS FORMAT.
Docs and people over internet don't make it clear, but to set Format only works on this form.
And to grab this to Entity, you need to write your own Hydrator extending the DoctrineHydrator:
namespace Application\Hydrator;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject;
class MyCustomHydrator extends DoctrineObject {
protected function handleTypeConversions($value, $typeOfField)
{
if($typeOfField == 'datetime'){
return \DateTime::createFromFormat('d/m/Y', $value);
}
return parent::handleTypeConversions($value, $typeOfField);
}
}
It's make it simple to work with any date format. You can extend further making Locale assertions on this Custom Hydrator as you want.