我是 Laravel 的新手,一直在尝试找出处理简单任务的最佳方法。我有一个视图,它有一个用于添加或编辑事件的表单(对于这个例子)。当表单将用于添加事件时,所有输入字段都是空白的。当它用于编辑事件时,输入由 MySQL 中的列填充。
我遇到的棘手问题是找出告诉视图我想在验证失败时使用旧表单输入或在我们第一次打开表单进行编辑时使用 MySQL 数据的最佳方法.
这是我目前在控制器中的内容,它很长,我相信有更好的方法,只是不确定它会是什么。
本质上,我将每个表单值分配给一个多维数组,然后将该数组返回给视图。每个数组值都是有条件的 - 如果有旧的输入值,请使用它。否则,使用表中的值。
public static function action_edit_event($id, $slug) {
$eventDetails = Calendar::find($id);
$event['id'] = $id;
$event['slug'] = $slug;
$event['title'] = (Input::old('admin_events-form-title')) ?
Input::old('admin_events-form-title') : $eventDetails->title;
$event['type'] = (Input::old('admin_events-form-type')) ?
Input::old('admin_events-form-type') : $eventDetails->calendartype_id;
$event['date_start'] = (Input::old('admin_events-form-date-start')) ?
Input::old('admin_events-form-date-start') : $eventDetails->start_date;
$event['allDay'] = (Input::old('admin_events-form-all-day')) ?
Input::old('admin_events-form-all-day') : $eventDetails->day;
$event['time-start_hour'] = (Input::old('admin_events-form-time-start_hour')) ?
Input::old('admin_events-form-time-start_hour') : (($eventDetails->start_time != null) ?
date("H", strtotime($eventDetails->start_time)) : '');
$event['time-start_minute'] = (Input::old('admin_events-form-time-start_minute')) ?
Input::old('admin_events-form-time-start_minute') : (($eventDetails->start_time != null) ?
date("i", strtotime($eventDetails->start_time)) : '');
$event['date_end'] = (Input::old('admin_events-form-date-end')) ?
Input::old('admin_events-form-date-end') : $eventDetails->end_date;
$event['time-end_hour'] = (Input::old('admin_events-form-time-end_hour')) ?
Input::old('admin_events-form-time-end_hour') : (($eventDetails->end_time != null) ?
date("H", strtotime($eventDetails->end_time)) : '');
$event['time-end_minute'] = (Input::old('admin_events-form-time-end_minute')) ?
Input::old('admin_events-form-time-end_minute') : (($eventDetails->end_time != null) ?
date("i", strtotime($eventDetails->end_time)) : '');
$event['description'] = (Input::old('admin_events-form-description')) ?
Input::old('admin_events-form-description') : $eventDetails->description;
return View::make('admin.event-post')->with('event', $event);
}
和视图中的表格..
{{ Form::open('admin/events/event', 'POST', array('id' => 'admin_events-form')) }}
<fieldset>
<legend>Event Details</legend>
<p>
{{ Form::label('admin_events-form-title', 'Event Title:')}}
{{ Form::text('admin_events-form-title', isset($event) ? $event['title'] : '') }}
{{ $errors->first('title', '<span class="ui-state-error">:message</span>') }}
</p>
<p>
{{ Form::label('admin_events-form-type', 'Event Type:') }}
{{ Form::select('admin_events-form-type', $select_event_types, isset($event) ? $event['type'] : '') }}
</p>
<p>
{{ Form::label('admin_events-form-date-start', 'Start Date:') }}
{{ Form::text('admin_events-form-date-start', isset($event) ? $event['date_start'] : '', array('id' => 'date_start')) }}
{{ Form::checkbox('admin_events-form-all-day', '1', (isset($event) && $event['allDay'] == 1) ? true : false, array('id' => 'allDay')) }}
{{ $errors->first('start_date', '<span class="ui-state-error">:message</span>') }}
</p>
<p>
{{ Form::label('admin_events-form-time-start_hour', 'Start Time:') }}
{{ Form::select('admin_events-form-time-start_hour', $select_hours, (isset($event) && $event['time-start_hour'] != null) ? $event['time-start_hour'] : '', array('id' => 'start-time_hour')) }} :
{{ Form::select('admin_events-form-time-start_minute', $select_minutes, (isset($event) && $event['time-start_minute'] != null) ? $event['time-start_minute'] : '', array('id' => 'start-time_minute')) }}
{{ $errors->first('start-time-hour', '<span class="ui-state-error">:message</span>') }}
{{ $errors->first('start-time-minute', '<span class="ui-state-error">:message</span>') }}
</p>
<p>
{{ Form::label('admin_events-form-date-end', 'End Date:') }}
{{ Form::text('admin_events-form-date-end', isset($event) ? $event['date_end'] : '', array('id' => 'date_end')) }}
<a href="#" id="admin_events-clear-date-end" class="button" title="Clear End Date">X</a>
<span class="ui-state-highlight">(if different from start date)</span>
</p>
<p>
{{ Form::label('admin_events-form-time-end_hour', 'End Time:') }}
{{ Form::select('admin_events-form-time-end_hour', $select_hours, (isset($event) && $event['time-end_hour'] != null) ? $event['time-end_hour'] : '', array('id' => 'end-time_hour')) }} :
{{ Form::select('admin_events-form-time-end_minute', $select_minutes, (isset($event) && $event['time-end_minute'] != null) ? $event['time-end_minute'] : '', array('id' => 'end-time_minute')) }}
{{ $errors->first('end-time-hour', '<span class="ui-state-error">:message</span>') }}
{{ $errors->first('end-time-minute', '<span class="ui-state-error">:message</span>') }}
</p>
<p>
{{ Form::label('admin_events-form-description', 'Event Description:') }}
{{ Form::textarea('admin_events-form-description', isset($event) ? $event['description'] : '', array('rows' => '5')) }}
</p>
<p class="center">
{{ $errors->first('description', '<span class="ui-state-error">:message</span>') }}
</p>
</fieldset>
{{ Form::hidden('mode', isset($event) ? 'edit' : 'add') }}
@if (isset($event))
{{ Form::hidden('slug', $event['slug']) }}
{{ Form::hidden('id', $event['id']) }}
@endif
{{ Form::close() }}
任何建议将不胜感激。谢谢!