Problem updating a date field

chris.paschen

Chris Paschen
I have an element that is of the type "date".

I need to manually push the current date to this field is a piece of code on a form plugin.

The PHP code is running onBeforeProcess (although I've tried onBeforeStore also with similar results)

The code that I'm running is:

$now = new DateTime();
$formModel->formData['ked_books_reviews_requests_49_repeat___date_accepted'] = $now;

If I run a dump of the value right after that line I can see that the current date has been set into that element; however, after saving the form the date isn't there (just 0000-00-00 etc.)

I've also tried using:

$formModel->updateFormData('ked_books_reviews_requests_49_repeat___date_accepted_raw',$now,true);

but nothing seems to actually get the value to store to the field in the table.

I have a number of similar date (datetime) fields that need to be via code, but I can't seem to get them to set via updateFormData or pushing into the field via formModel->formData.

I'm guessing I just have a syntax (or process script location) error.

Any ideas how to make this work?
 
You are setting it to a DateTime object. You need to set it to an actual formatted string, in standard MySQL date/time format:

Code:
$now = new DateTime();
$formModel->formData['ked_books_reviews_requests_49_repeat___date_accepted'] = $now->format('Y-m-d H:i:s');

I suspect when you dump the value for debugging, it is automatically invoking the "magic string" method of the DateTime object, so you are seeing a textual representation of the object.

-- hugh
 
I'm using the J!DUMP extension to dump out the values - so yes, probably they are formatting it somehow.

The syntax you provided worked. Thanks.

BTW ... (for anyone searching later) ... one other date field I was working on wasn't getting set and I was about to post back here when I took another look and saw that the element was unpublished in the specific list that I was using for this form (it was a 2nd 'view' onto the table than the original one and I had unpublished a number of the elements to have them not appear).
Just remember to make sure everything is published that you need to work with in the current group or you won't be able to set it.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top