8

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.

Phil
  • 2,176
  • 10
  • 33
  • 56

4 Answers4

20

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!

Khalid Khan
  • 3,017
  • 1
  • 11
  • 27
Zach Kauffman
  • 486
  • 1
  • 4
  • 17
  • I will try using this solution to identify the attendee_id to open the new modal. It tries to open a modal for each attendee otherwise because I have a list of attendees with edit buttons in the main page. – Phil Dec 24 '14 at 17:52
  • I had to use Session:get to get my variable. Not sure how you got it to do it without. I updated my question to show what I eventually did. Also my $error messages did not pass to my modal. I will ask a new question to get help with that. – Phil Dec 29 '14 at 02:45
  • Yeah, you have to use Session::get because when you use Redirect it will overwrite the current flash messages and pass them to the $old array. – Zach Kauffman Dec 29 '14 at 19:42
  • Doesn't the check for 5 make `if(!empty(Session::get('error_code'))` redundant? If it equals 5 it's not going to be empty. – Hashim Aziz Jan 17 '22 at 16:54
1

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
prograhammer
  • 20,132
  • 13
  • 91
  • 118
  • Very helpful but didn't work because it opened a modal for each button which breaks the modal function. I am not sure there is a way to fire the event for the one I clicked prior... Also i think you made a typo replaced `.click` with `.trigger("click");` – Phil Dec 24 '14 at 17:43
  • i think I can use suggestion by @Zach to return the attendee_id to identify the button somehow – Phil Dec 24 '14 at 17:47
  • 1
    oh gotcha...regarding the trigger, you can simply use .click as well and it's a shortcut in this case. – prograhammer Dec 30 '14 at 19:55
1

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

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!

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102