【问题标题】:Laravel 5.4 redirect to modal after validationLaravel 5.4 验证后重定向到模态
【发布时间】:2017-11-30 01:41:36
【问题描述】:

我有一个模态表单,我想重定向到所有错误的模态,但它似乎不起作用,这是我在控制器中的验证器

   $validator = Validator::make($request->all(), [
        'company' => 'required|string|max:255',
        'description' => 'required|string|max:300',
        'engineers' => 'required',
    ]);

    if ($validator->fails()) {
        return redirect()->back()->withErrors($validator)->withInput();
    }

这是我在刀片视图中的 jquery,其中我的模态被激活

 @if (count($errors) > 0)
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    $('#create').modal('show');   
</script>
@endif

我的模态原型

<div class="modal fade container" id="createProject">
   {!! Form::open([ 'route' => 'projects.store']) !!}
        <div class="modal-content" id="create">
    @if($errors->has())
        @foreach ($errors->all() as $error)
            <div>{{ $error }}</div>
        @endforeach
    @endif
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Create Project</h4>
        </div>
        <div class="modal-body">

发生的情况是验证失败后,它只是重定向回调用它的视图,但我必须再次单击才能触发模态

【问题讨论】:

  • 尝试将jquery中的id改成createProject并检查
  • 而不是提交表单并重定向回表单并显示模式。这个过程很长,只需使用将在后台发送请求的 AJAX,您无需在提交后显示模式,因为它发生在后台。

标签: javascript php jquery laravel laravel-5.4


【解决方案1】:

改变这部分:

 @if (count($errors) > 0)
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    $('#create').modal('show');   
</script>
@endif

 @if (count($errors) > 0)
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

$(document).ready(function(){

 $('#create').modal('show'); 

});
@endif

为什么不使用 AJAX 提交表单并在模态仍然打开时返回错误。

【讨论】:

    【解决方案2】:

    改变这一行

    $('#create').modal('show');   
    

    到这一行

     (function($){
           $('#createProject').modal('show');  
        })($);
    

    id 你用来显示模型在这里是错误的。而且您正在调用模型打开,如果 DOM 尚未准备好,它可能无法正常工作。所以用上面的代码换行,它应该可以正常工作了。

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 1970-01-01
      • 2018-03-03
      • 2020-11-02
      • 2017-10-20
      • 2019-03-18
      • 2017-06-29
      • 2018-01-12
      • 2018-05-04
      相关资源
      最近更新 更多