Laravel如何重定向回Boostrap模式对话框窗口

Laravel如何重定向回Boostrap模式对话框窗口

本文介绍了Laravel如何重定向回Boostrap模式对话框窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想返回模态对话框编辑表单以显示验证错误,但是使用Redirect::back,我最终进入没有模态窗口的HTML页面.

I want to return to my modal dialog edit form to show validation errors but using Redirect::back I just end up at the HTML page without the modal window.

我使用 BootstrapDialog 将我的attendee.edit路由加载到弹出式对话框中编辑表格.

I use BootstrapDialog to load my attendee.edit route into a modal dialog to that pops up an edit form.

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页面而没有对话框.如何重定向回到模态窗口?

After I edit the form I want to return to the modal window to display validation errors but I just end up at the HTML page without the dialog. How do I redirect back to the modal window?

更新

在控制器返回中添加了id

added id to controller return

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

如果出现错误,这将重新打开模式窗口,如果没有,则继续返回.我不喜欢它如何关闭和重新打开模态,并且验证错误不会传递给模态,所以我不建议这样做.

This reopens the modal window if there was an error but proceeds back if not. I don't like how it closes and reopens the modal and the validation errors are not passed to the modal so I would not recommend doing it this way.

推荐答案

我只是做了这样的事情.您只需要使用Blade的模板即可!

I just did something like this. You just need to use blade's templating!

//pass back a variable when redirecting
return Redirect::back()->with('error_code', 5);

然后在您的刀片服务器模板中:

And then in your blade template:

@if(!empty(Session::get('error_code')) && Session::get('error_code') == 5)
<script>
$(function() {
    $('#myModal').modal('show');
});
</script>
@endif

只要有error_code等于5,就会打开对话框!

This will opens the dialog whenever there is error_code is present and equals to 5!

这篇关于Laravel如何重定向回Boostrap模式对话框窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:03