モーダルダイアログ編集フォームに戻って検証エラーを表示したいのですが、使用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
これにより、エラーが発生した場合はモーダルウィンドウが再び開きますが、そうでない場合は元に戻ります。モーダルを閉じて再度開く方法が気に入らず、検証エラーがモーダルに渡されないため、この方法はお勧めしません。
私はちょうどこのようなことをしました。ブレードのテンプレートを使用するだけです!
//pass back a variable when redirecting
return Redirect::back()->with('error_code', 5);
そして、ブレードテンプレートで:
@if(!empty(Session::get('error_code')) && Session::get('error_code') == 5)
<script>
$(function() {
$('#myModal').modal('show');
});
</script>
@endif
これにより、error_codeが存在し、5に等しい場合は常にダイアログが開きます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加