2013-02-01 43 views
1

我是Laravel的新手,一直試圖找出處理簡單任務的最佳方法。我有一個視圖,它具有用於添加或編輯事件的窗體(對於此示例)。當表單將用於添加事件時,所有輸入字段都是空白的。當它用於編輯一個事件時,輸入由MySQL的列填充。基於驗證,在顯示錶單時使用表格中的存儲值或提交的表單數據

我遇到了棘手位搞清楚最好的方式告訴我想如果驗證失敗,要麼使用舊的形式輸入或者使用MySQL的數據,如果我們打開表單視圖第一次進行編輯。

這是我在控制器中的那一刻,它非常長,我確信有更好的方法,只是不知道它會是什麼。

本質上,我將每個表單值分配給多維數組,然後將該數組返回給View。每個數組值都是有條件的 - 如果存在舊的輸入值,則使用該值。否則,請使用表中的值。

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() }} 

任何意見將不勝感激。謝謝!

回答

2

我假設你用於實際驗證和存儲事件的代碼是在控制器的另一個方法中(即action_save_event)?

如果驗證失敗,我會簡單地將該視圖與來自該方法的輸入數據一起返回。

例如:

public function action_edit_event($id, $slug) 
{ 
    $eventDetails = Calendar::find($id); 
    $event['id'] = $id; 
    $event['slug'] = $slug; 
    return View::make('admin.event-post')->with('event', $event); 
} 

public function action_store_event($id, $slug) 
{ 
    // Validate the input data 
    // If valid store to the database 
    // Else, assemble $event from the old input data 
    $event['id'] = $id; 
    $event['slug'] = $slug; 
    $event['title'] = Input::old('admin_events-form-title'); 
    $event['type'] = Input::old('admin_events-form-type'); 
    // ... 
    // Return the view with $event 
    return View::make('admin.event-post')->with('event', $event); 
} 
+0

謝謝!讓我走上想法的正確軌道:) – NightMICU