【问题标题】:Laravel how to redirect back to boostrap modal dialog windowLaravel如何重定向回引导模式对话框窗口
【发布时间】:2015-02-22 17:12:44
【问题描述】:

我想返回到我的模态对话框编辑表单以显示验证错误,但使用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>

JQuery 调用 BootstrapDialog

$(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

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

【问题讨论】:

    标签: laravel bootstrap-dialog


    【解决方案1】:

    我刚刚做了这样的事情。你只需要使用刀片的模板!

    //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,这将打开对话框!

    【讨论】:

    • 我将尝试使用此解决方案来识别attendee_id 以打开新模式。否则它会尝试为每个与会者打开一个模式,因为我在主页上有一个带有编辑按钮的与会者列表。
    • 我不得不使用 Session:get 来获取我的变量。不知道你是怎么做到的。我更新了我的问题以显示我最终做了什么。我的 $error 消息也没有传递给我的模态。我将提出一个新问题以获得帮助。
    • 是的,你必须使用 Session::get 因为当你使用 Redirect 时它会覆盖当前的 flash 消息并将它们传递给 $old 数组。
    • 对 5 的检查不会使if(!empty(Session::get('error_code')) 变得多余吗?如果它等于 5,则它不会为空。
    【解决方案2】:

    这对我有用。

    在我的模态中:

    <div class="modal-content">
    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <strong>Error</strong><br><br>
            <ul>
                @foreach ($errors->all() as $error)
                   <li>{{ $error }}</li>
                @endforeach
            </ul>
      </div>
    @endif
    <div class="modal-body">
    .... rest of modal window code
    

    在我看来:

    <script type="text/javascript">
        @if (count($errors) > 0)
            $('#modal').modal('show');
        @endif
    </script>
    

    在我的控制器中,我只是像往常一样重定向回视图。

    【讨论】:

      【解决方案3】:

      我唯一能想到的就是这样设置:

      `<input type="hidden" name="show" value="{{ Input::old('show') ? Input::old('show'):"" }}" />` 
      

      当页面被 get 请求加载时,这个值将被设置为“”,因为Input::old('show') 没有被设置(当然是被设置为“”)。然后,当您发布表单时,将此输入的值更改为:

      $("input[name=show"]").val("yes");
      

      因此,如果/当页面重定向时,此隐藏输入的值将设置为“yes”而不是“”。然后,用 Javascript 处理:

      $(document).ready(function(){
        if($("input[name=show]").val() != "")){
          $('#modal_id').modal('show');
        }
      });
      

      希望这是有道理的!

      【讨论】:

        【解决方案4】:

        如果在会话中发现错误,您的视图中似乎需要一个条件来打开对话框:

        @if($errors->any())
          $('.btn.edit-attendee').click();
        @endif
        

        【讨论】:

        • 非常有帮助,但没有奏效,因为它为每个按钮打开了一个模式,这打破了模式功能。我不确定有没有办法为我之前点击的那个事件触发事件......另外我认为你用.trigger("click");替换了一个错字.trigger("click");
        • 我想我可以使用 @Zach 的建议返回参加者 ID 以某种方式识别按钮
        • 哦,明白了...关于触发器,您也可以简单地使用 .click ,在这种情况下它是一个快捷方式。
        猜你喜欢
        • 1970-01-01
        • 2013-05-05
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        • 1970-01-01
        • 1970-01-01
        • 2015-01-08
        • 1970-01-01
        相关资源
        最近更新 更多