Laravel 如何重定向回 boostrap 模态对话框窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27639364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:50:24  来源:igfitidea点击:

Laravel how to redirect back to boostrap modal dialog window

laravelbootstrap-dialog

提问by Phil

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

我想返回到我的模态对话框编辑表单以显示验证错误,但使用Redirect::back我只是在没有模态窗口的 HTML 页面上结束。

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

我使用BootstrapDialog将我的attendee.edit路线加载到一个模态对话框中,以弹出一个编辑表单。

HTML

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 call to BootstrapDialog

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

Controller

控制器

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');
}

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?

编辑表单后,我想返回到模态窗口以显示验证错误,但我只是在没有对话框的 HTML 页面上结束。如何重定向回模态窗口?

UPDATE

更新

added idto controller return

添加id到控制器返回

return Redirect::back()->withErrors($validator)->withInput()->with('id', $id);

Added Jquery

添加了 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.

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

回答by Zach Kauffman

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

我只是做了这样的事情。你只需要使用blade的模板!

//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

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

只要存在 error_code 且等于 5,就会打开对话框!

回答by prograhammer

Sounds like you need a condition in your view to open the dialog if errors are found in the session:

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

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

回答by Leonard Harley

This worked for me.

这对我有用。

In my modal:

在我的模态中:

<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

In my view:

在我看来:

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

In my controller I simply redirected back to the view as normal.

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

回答by Tim Lewis

The only thing I could think of is setting something like this:

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

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

When the page is loaded by a get request, this value will be set to "", as Input::old('show')isn't set (well is but is set to ""). Then, when you post the form, change the value of this input to:

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

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

So that if/when the page redirects, the value of this hidden input will be set to "yes" instead of "". Then, handle that in Javascript:

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

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

Hope that makes sense!

希望这是有道理的!