Question

Project utilizing ZF2 + Doctrine 2.

I Tried many formats. I'm working with a Form without validation.

My last try was:

    $traindate = new Element\DateTime('trainDate');
    $traindate->setAttributes(array(
        'name' => 'trainDate',
        'id' => 'trainDate',
        'size' => '30',
        'class' => 'datepicker',
        'options' => array(
            'label' => '',
            'format' => 'd-m-Y H:i'
        ),
    ));

I need to use a input to set a date and time of a event. On Brazil the basic format is: 14-05-2014 14:20 15-05-2015 15:00

With means Days Months Year Hour Minutes, like I'm expressing on the Options -> Format.

This way always when I try to insert, i get the following messsage: The input does not appear to be a valid date

Removing the format, i can only pass by $form->isValid($data) by Y-m-d (American Format), but by the way i can't pass time to date too, which is causing me big troubles.

I need to set date PT_BR on Form/Input, Pass by validation, Convert Data do Mysql Format (YYYY-mm-dd HH:ii:ss). And then retrive from db and convert back to pt_br format.

But not even i can pass time with date to zf2 form, ever this error message.

I remove all filters from this form trying to get work, but doesn't work.

Where is the main problem?

Was it helpful?

Solution

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.

OTHER TIPS

I recommend you to do the date conversion work in your Train entity. The trainData's property getter and setter could be:

//THE PROPERTY IS IN MYSQL FORMAT, BUT THE GETTER WILL RETURN IT ALWAYS IN THE BRAZILIAN ONE
public function getTrainDateTime() {
    return $this->trainDateTime->format( 'd/m/Y H:i' );
}

//IT WILL ALWAYS RECIEVE A BRAZILIAN DATETIME, CONVERT IT TO MYSQL FORMAT
public function setTrainDateTime( $trainDateTime ) {
    $time = \DateTime::createFromFormat( 'd/m/Y H:i', $trainDateTime )->getTimestamp();

    $this->trainDateTime = new \DateTime( date( 'Y-m-d', $time ) );

    return $this;
}

If you do it this way, you can always work freely with brazilian dates, without worring about formats. The dirty work will be done by the entity class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top