I have problem with Carbon
and HTML5 input input[type=datetime-local]
because this input sends datatime in format Y-m-d\TH:i
(eg. 2016-11-20T11:45
).
I have method in my controller:
public function store(ModelStoreFormRequest $request)
{
$model = new Model($request->all());
$model->save();
return redirect->action(/*...*/);
}
And I get exception:
InvalidArgumentException in Carbon.php line 582: Data Missing
1. in Carbon.php line 582
2. at Carbon::createFromFormat('Y-m-d H:i:s', '2016-11-20T11:45') in HasAttributes.php line 709
So I have solved this problem by creating next function in my model:
public function setStartedAtAttribute($startedAt)
{
if( $startedAt instanceof Carbon ) {
$this->attributes['started_at'] = $startedAt;
return;
}
if( strpos($startedAt, 'T' ) ) {
$this->attributes['started_at'] = Carbon::createFromFormat('Y-m-d\TH:i', $startedAt);
return;
}
$this->attributes['started_at'] = Carbon::createFromFormat('Y-m-d H:i:s', $startedAt);
}
But I do not like this solution, I wonder if there are more elegant solution? I am thinking to use ModelStoreFormRequest::prepareForValidation()
method and there to check if format of date is: Y-m-d\TH:i
to change datetime value to format: Y-m-d H:i:s
, or maybe to use Carbon::parse()
method like this:
protected function prepareForValidation()
{
$input = $this->all();
$input['started_at'] = \Carbon\Carbon::parse($input['started_at']);
$this->replace($input);
}
But I still do not know is this solution fine. I am trying to separate concerns and obligation of each class... What do you suggest? Any other more elegant solution or stick to the current one?
new Carbon($startedAt)
? – CrannogCarbon::createFromFormat('Y-m-d H:i:s')
but what you are suggesting is to just usenew carbon
instead ofCarbon::parse()
, buy still leaves me with problem should that go into setter,FormRequest::preapreForValidation
or somewhere else... – Savory