Created
August 25, 2014 17:33
-
-
Save fabiopaiva/fd8ff072c8c53a2ae4bd to your computer and use it in GitHub Desktop.
Date in d/m/Y format ZF2 + Doctrine + Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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