Laravel how to redirect back to boostrap modal dialog window

前端 未结 4 1449
时光取名叫无心
时光取名叫无心 2021-02-06 07:55

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.

I

相关标签:
4条回答
  • 2021-02-06 08:09

    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
    

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

    0 讨论(0)
  • 2021-02-06 08:11

    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.

    0 讨论(0)
  • 2021-02-06 08:19

    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
    
    0 讨论(0)
  • 2021-02-06 08:31

    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:

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

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

    Hope that makes sense!

    0 讨论(0)
提交回复
热议问题