モーダル ダイアログの編集フォームに戻って検証エラーを表示したいのですが、使用するRedirect::back
とモーダル ウィンドウのない HTML ページになってしまいます。
BootstrapDialogを使用 してルートをモーダル ダイアログに読み込み、attendee.edit
編集フォームをポップアップします。
HTML
<td>{{link_to_route('attendee.edit','',array($attendee->id), array(
'class'=>'edit-attendee btn btn-info btn-xs glyphicon glyphicon-pencil',
'data-title' => 'Edit Attendee'))}}
</td>
BootstrapDialog への JQuery 呼び出し
$(document).ready(function(){
$('.btn.edit-attendee').click(function(e){
e.preventDefault();
url = $(this).attr('href');
BootstrapDialog.show({
title: $(this).data('title'),
message: $('<div></div>').load(url),
buttons: [{
label: 'Update',
action: function(dialogRef) {
$('form').submit();
}
}]
});
});
});
コントローラ
public function update($id)
{
$attendee = Attendee::findOrFail($id);
$validator = Validator::make($data = Input::all(), Attendee::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$attendee->update($data);
return Redirect::route('attendees.index');
}
フォームを編集した後、モーダル ウィンドウに戻って検証エラーを表示したいのですが、ダイアログのない HTML ページになってしまいます。モーダル ウィンドウにリダイレクトするにはどうすればよいですか?
アップデート
id
コントローラーリターンに追加
return Redirect::back()->withErrors($validator)->withInput()->with('id', $id);
Jqueryを追加
@if(!empty(Session::get('id')))
$(document).ready(function(){
url = "{{ URL('attendee') . '/' . Session::get('id'). '/edit' }}";
BootstrapDialog.show({
title: $(this).data('title'),
message: $('<div></div>').load(url),
buttons: [{
label: 'Update',
action: function(dialogRef) {
$('form').submit();
}
}]
});
});
@endif
これにより、エラーが発生した場合はモーダル ウィンドウが再度開きますが、そうでない場合は元に戻ります。モーダルを閉じて再度開く方法が気に入らず、検証エラーがモーダルに渡されないため、この方法はお勧めしません。