This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi,
Has anyone managed to get this working in a plugin etc? Whenever I try to use mode: time
on a datepicker, I get the error:
"Unexpected data found.
Unexpected data found.
Data missing"
on line 393 of /Applications/MAMP/htdocs/october/vendor/nesbot/carbon/src/Carbon/Carbon.php
when trying to save the model.
I've tried changing the time field type in the database to TIMESTAMP and TIME and both return errors.
I've also added the fields to the $dates
attribute in the model but still get the error.
fields.yaml :
start_time:
label: Start Time
type: datepicker
mode: time
span: left
comment: (if applicable)
Any help would be appreciated as always.
Last updated
I was having the same issue, here's what I've found out. October uses Carbon (http://carbon.nesbot.com/) for datetime functions and Carbon expects any item that is in $protected $dates[] to be in a specific format (Y-m-d H:i:s), the problem is when saving a "time" field (as defined in your fields.yaml file) it will save time as H:i:s, without the Y-m-d and when your model fetches that field and Carbon tries to parse the date, it barfs. My solution is to transform the time fields in the model to a format that Carbon can accept, and THEN tell the model that these fields are date fields. i.e.
public function afterFetch(){
$this->start_time = date('Y-m-d H:i:s', strtotime('0-0-0 ' . $this->start_time));
$this->end_time = date('Y-m-d H:i:s', strtotime('0-0-0 ' . $this->end_time));
$this->dates[] = 'start_time';
$this->dates[] = 'end_time';
}
Hope that helps you.
Last updated
@nastysquar3d your answer is correct. As an alternative solution you can also specify mutators on the model for each date attribute you use, as well as specifying the name of the field inside the $dates
array on the model. I prefer this rather than modifying input that has not been yet validated, when the mutator method runs we know at this point it is ready to insert into the database. Here is an example of a mutator I used which worked for me:
public function setDateOfBirthAttribute($value)
{
// Create from whichever format you're using, so in your case Y-m-d
$this->attributes['date_of_birth'] = Carbon::createFromFormat('Y-m-d', $value);
}
Last updated
1-7 of 7