laravelbootstrap-dialog

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.


Solution

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