Reputation: 2042
My model has an event_datetime field which I want to set on the form using datetime select fields. Rails has datetime_select which outputs the following and saves to a MySQL datetime column.
Must default to the current date and work on a shared add/edit form.
No javascript datepickers please.
Upvotes: 0
Views: 2184
Reputation: 22872
Add this to you
{{ Form::datetime('event_date') }}
And place this macro into some autoloaded file (or view file)
Form::macro('datetime', function($name) {
$years = value(function() {
$startYear = (int) date('Y');
$endYear = $startYear - 5;
$years = ['' => 'year'];
for($year = $startYear; $year > $endYear; $year--) {
$years[ $year ] = $year;
};
return $years;
});
$months = value(function() {
$months = ['' => 'month'];
for($month = 1; $month < 13; $month++) {
$timestamp = strtotime(date('Y'). '-'.$month.'-13');
$months[ $month ] = strftime('%B', $timestamp);
}
return $months;
});
$days = value(function() {
$days = ['' => 'day'];
for($day = 1; $day < 32; $day++) {
$days[ $day ] = $day;
}
return $days;
});
$hours = value(function() {
$hours = ['' => 'hour'];
for($hour = 0; $hour < 24; $hour++) {
$hours[ $hour ] = $hour;
}
return $hours;
});
$minutes = value(function() {
$minutes = ['' => 'minute'];
for($minute = 0; $minute < 60; $minute++) {
$minutes[ $minute ] = $minute;
}
return $minutes;
});
return Form::select($name.'[year]', $years) .
Form::select($name.'[month]', $months) .
Form::select($name.'[day]', $days) . ' - ' .
Form::select($name.'[hour]', $hours) .
Form::select($name.'[minute]', $minutes);
});
Upvotes: 5