1

I have a plugin that includes a datepicker.

I initialize the datepicker like this

javascript

$('.datepicker').datepicker({
    'format': 'mm/dd/yyyy',
    'todayHighlight': true
});

I am saving the date from my plugin like this:

$myDate = craft()->request->getPost('myDate');
if ($myDate) {
    DateTime::createFromString($myDate, craft()->timezone);
}

$fieldsLocation = craft()->request->getParam('fieldsLocation', 'fields');
$entry->setContentFromPost($fieldsLocation);

Because I have designated this field to be required, I get an error back saying that the field cannot be blank. Even though I have chosen a date.

If I change my datepicker to use this format:

$('.datepicker').datepicker({
    'format': 'yyyy/mm/dd',
    'todayHighlight': true
});

Everything works. I like how "human/U.S" friendly the top example is vs. yyyy-mm-dd.

Since I am populating the entry with this:

$entry->setContentFromPost($fieldsLocation);

Is there a way to convert what is passed in to the correct format Craft is looking for?

I've tried something like:

$date = date(str_replace('/', '-', craft()->request->getPost('myDate')));

$myDate = date('Y-m-d', strtotime($date));

But that is getting overwritten by this guy still:

entry->setContentFromPost($fieldsLocation);

Thank you for any suggestions!

1

After $entry->setContentFromPost($fieldsLocation); you should be able to add another line to explicitly set your calculated date.

$entry->setContent(array('fieldName' => $myDate));

Then just proceed with saving the entry as normal.

| improve this answer | |
  • Thank you! I didn't realize I could call setContent back to back like that. – Damon Jan 17 '17 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.