Skip to content

Instantly share code, notes, and snippets.

@fabiopaiva
Created August 25, 2014 17:33
Show Gist options
  • Save fabiopaiva/fd8ff072c8c53a2ae4bd to your computer and use it in GitHub Desktop.
Save fabiopaiva/fd8ff072c8c53a2ae4bd to your computer and use it in GitHub Desktop.
Date in d/m/Y format ZF2 + Doctrine + Form
<?php
namespace Application\Hydrator;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject;
class DateHydrator extends DoctrineObject {
protected function handleTypeConversions($value, $typeOfField) {
if ($typeOfField == 'date') {
if ($value == "") {
return null;
} else {
return \DateTime::createFromFormat('d/m/Y', $value);
}
}
return parent::handleTypeConversions($value, $typeOfField);
}
}
<?php
namespace Application\Form;
use Zend\Form\Form;
use Application\Hydrator\DateHydrator as Hydrator;
class MyForm extends Form {
public function __construct($name, $options = array()) {
$this
->setHydrator(new Hydrator($options['om']->get('Doctrine\ORM\EntityManager')))
->add(array(
'name' => 'data',
'type' => 'datetime',
'options' => array(
'label' => 'Data',
'format' => 'd/m/Y'
),
'attributes' => array(
'required' => true,
'class' => 'form-control input-sm date'
)
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment