2

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.

Anonymous
  • 6,181
  • 7
  • 45
  • 72
  • 1
    What does it do instead? Are you getting some error? Not working is not saying much, you know? – hakre Jan 07 '13 at 10:53
  • @hakre - Apologies, I should have explained. It simply continues to show what I presume is the default date for the datetime field - 2008-01-01 00:00 – Anonymous Jan 07 '13 at 10:55
  • 1
    Normally not, please run code like this verbatim on your system and see if you're not getting now. http://eval.in/6082 – hakre Jan 07 '13 at 10:58
  • @hakre - Output: object(DateTime)#1 (3) { ["date"]=> string(19) "2013-01-07 11:00:10" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London" } – Anonymous Jan 07 '13 at 11:00
  • 1
    Are you reading/importing the 2008 date from other objects that overwrite it later? E.g. probably with database access or entities that have a default value? – hakre Jan 07 '13 at 11:02
  • @hakre - This form is for adding new 'tasks' to a task list. There are no other entities involved and no default values are set in the Task entity class. Should I be setting the default value somehow in the Entity class? – Anonymous Jan 07 '13 at 11:03
  • 1
    Sure, otherwise I'd say it overwrites it. See as well [Set default value on Datetime field in symfony2 form](http://stackoverflow.com/questions/8713200/set-default-value-on-datetime-field-in-symfony2-form) – hakre Jan 07 '13 at 11:05
  • @hakre - Got it working now. It turns out Firefox was giving me a cached version of the page. Ctrl+F5 and it's working. Rookie mistake. :-( lol. – Anonymous Jan 07 '13 at 11:06
  • 1
    lol, yeah that happens. I have a no-chache setting in firefox with the webdeveloper toolbar so it does not happen clearly ;) for local development, the speed is still okay even w/o caching then. – hakre Jan 07 '13 at 11:08
  • 1
    But anyway, please keep the question and answer it. So others who look can gain some confidence. Put your working code into the answer as well. – hakre Jan 07 '13 at 11:09
  • All done, thanks for your help hakre. – Anonymous Jan 07 '13 at 11:57
  • @hakre - I have to wait 2 days to accept my own answer apparently, will come back to this question then. – Anonymous Jan 07 '13 at 13:11
  • Oh, right, yeah ;) My fault I thought this was possible faster. – hakre Jan 07 '13 at 13:12

1 Answers1

4

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.

Anonymous
  • 6,181
  • 7
  • 45
  • 72
  • 2
    Doing that in the class constructor might have side effects or lead to wrong results. Remember that the class constructor is also executed when Doctrine loads your entities from the database. – likeitlikeit May 08 '13 at 14:07
  • How do you solve the issue @likeitlikeit because I'm stuck with current time when editing? – Masinde Muliro Mar 02 '14 at 03:18
  • using 'data' option will set the field to datetime now even on edits. This is not always desired. – Forer Feb 20 '17 at 17:01