Can't store data in Date attribute programmatically

Permalink
Hi all

This is a slightly different version of my other problem with dates.

I have a single-page editing interface where I create pages, assigning attributes as I go. I want a datetime() picker which will specify a date attribute called 'closing_date', but I can't make the data stick.

I can save the page's cDatePublic with no trouble, but there seems to be a problem with saving data to an attribute of type date.

Here's the relevant part of the form:

<?php echo $form->label('jobClosingDate', t('Closing date'))?><?php echo $date->datetime('jobClosingDate', '' ); ?>


..I can't grab the existing value either, hence the null value in datetime() - I get this if I do:

Warning: strtotime() expects parameter 1 to be string, object given in /public_html/cms/concrete/helpers/form/date_time.php on line 81

Warning: strtotime() expects parameter 1 to be string, object given in /public_html/cms/concrete/helpers/form/date_time.php on line 82

Warning: strtotime() expects parameter 1 to be string, object given in /public_html/cms/concrete/helpers/form/date_time.php on line 83

Warning: strtotime() expects parameter 1 to be string, object given in /public_html/cms/concrete/helpers/form/date_time.php on line 84

Am tearing my hair out over this, and that's before I deal with European dates!

melat0nin
 
andrew replied on at Permalink Reply
andrew
How are you trying to save the date? Are you using the DateTimeHelper::translate() function to translate the post into a valid date string?
melat0nin replied on at Permalink Reply
melat0nin
Hi Andrew

I don't think I am, but I'm not sure where to do it.

I have the form in view.php which has the code above, then in controller.php I'm using tweaked code from your one-page editing interface howto:

private function saveData($p) {
      Loader::model("attribute/categories/collection");
      [...]
      $j_closing = CollectionAttributeKey::getByHandle('closing_date');
         $j_closing->saveAttributeForm($p);
      [...]
}
public function update() {
      $this->edit($this->post('jobID'));
      if($this->isPost()){
         $p = Page::getByID($this->post('jobID'));
         $updatedPageData = array('cName' => $this->post('pageName'), 'cDescription' => $this->post('pageDesc'));
         $p->update($updatedPageData);
         $this->saveData($p);
         $this->redirect('/dashboard/editor/', 'page_updated');


I'm able to store and update cDatePublic without any trouble, but it's a date attribute that I can't seem to store properly.
melat0nin replied on at Permalink Reply 2 Attachments
melat0nin
Any thoughts?

I've attached my /controllers/vacancies/controller.php and /single_pages/dashboard/vacancies/view.php files which might make it clearer. As you can see they are simplified versions of the files from your howto.

Cheers!
melat0nin replied on at Permalink Reply
melat0nin
Durrr sorry to bump but i'm in a bit of a pickle with this one. Any help would be much appreciated.
andrew replied on at Permalink Reply
andrew
Try the DateHelper::translate() function as detailed on this page:

http://www.concrete5.org/documentation/developers/forms/concrete5-w...

If you call the date helper with

$dateHelper->date('myDate');


then in your controller do this:

$myDate = $dateHelper->translate('myDate');
print $myDate;


and verify you have a valid date. Then try and setAttribute on it.
melat0nin replied on at Permalink Reply
melat0nin
EDIT: I think I've managed it - can store and update the date value now. Phew!

Hi Andrew

THanks for the reply. I'm still not able to set the attribute. I can set cDatePublic, but I can't set a custom date attribute. I've followed the same form that's used in your howto for other attributes (like text) but it doesn't work. I've sprinkled the Date Helper and translate method around a bit but I can't seem to store the value. I'm guessing I need to translate the value that's passed from the form in view.php ($jobClosingDate), but I can't grab that in controller.php (??).

Here's the form element in view.php:

$j_closing = CollectionAttributeKey::getByHandle('closing_date');
$closingvalue = $job_id->getAttributeValueObject($j_closing);
echo $form->label('jobClosingDate', t('Closing date'));
echo $date->datetime('jobClosingDate',$job_id->getCollectionAttributeValue($j_closing) );


then in controller.php:

private function saveData($p) {
...
$j_closing = CollectionAttributeKey::getByHandle('closing_date');
$j_closing->saveAttributeForm($p);
...
}


The closing_date attribute is a Date/Time attribute with Ask User For set to Date Only (although I've tried the others and I have the same problem).

Any ideas?

EDIT: i've just noticed that the form outputted by view.php includes this for the input for my attribute:

<input id="_dt" name="_dt" class="ccm-input-date" value="4/11/2011"  />


There's no variable name there, which can't be right. For example, jobDatePublic (which gets applied to cDatePublic) comes up as jobDatePublic_dt. The correct variable isn't being outputted in the form, and therefore won't be getting POSTed.