Anonymous
Anonymous

Reputation: 6251

How to set the default value of a datetime field to the current time in Symfony2?

How would I go about setting the default value of the datetime field of a form to the current time in Symfony2?

I'm using the FormBuilderInterface and the following isn't working:

$builder->add(
    'completed_datetime', 'datetime', array('data' => new \DateTime('now'))
);

The form continues to show what I presume to be the default value for the datetime field 2008-01-01 00:00.

Upvotes: 2

Views: 8394

Answers (1)

Anonymous
Anonymous

Reputation: 6251

In the end it turned out I was viewing a cached version of my page. Ctrl+F5 cleared the cache and showed the desired result.

I was able to achieve the functionality I wanted in both the ways posted here, i.e.:

In the Type class:

$builder->add(
    'completed_datetime', 'datetime', array('data' => new \DateTime('now'))
);

And in my controller:

$task->setCompletedDateTime(new \DateTime('now'));

I believe it's also possible to set it in my Task Entity class' constructor.

Upvotes: 4

Related Questions