Phil
Phil

Reputation: 2236

Laravel how to redirect back to boostrap modal dialog window

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

JQuery call to 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?

UPDATE

added id to controller return

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

Added 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.

Upvotes: 8

Views: 40726

Answers (5)

Abacus
Abacus

Reputation: 70

Thanks Leo, it works.

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

Upvotes: 0

Zach Kauffman
Zach Kauffman

Reputation: 496

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!

Upvotes: 20

Leonard Harley
Leonard Harley

Reputation: 36

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.

Upvotes: 1

Tim Lewis
Tim Lewis

Reputation: 29278

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!

Upvotes: 0

prograhammer
prograhammer

Reputation: 20590

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

Upvotes: 1

Related Questions