Laravel how to redirect back to dynamic boostrap dialog

I want to return to my modal dialog editing form to show validation errors, but using Redirect::backI'm just on an HTML page without a modal window.

I use BootstrapDialog to load my route attendee.editinto a modal dialog that issues 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 for 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 on the HTML page without a dialog. How to redirect back to the modal window?

UPDATE

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

, , , . , , , .

+4
4

- . !

//pass back a variable when redirecting
return Redirect::back()->with('error_code', 5);

:

@if(!empty(Session::get('error_code')) && Session::get('error_code') == 5)
<script>
$(function() {
    $('#myModal').modal('show');
});
</script>
@endif

script, , error_code, 5!

+9

, , , :

@if($errors->any())
  $('.btn.edit-attendee').click();
@endif
+1

.

:

<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

:

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

.

+1

, , - :

`<input type="hidden" name="show" value="{{ Input::old('show') ? Input::old('show'):"" }}" />` 

, ", Input::old('show') (, " "). , , :

$("input[name=show"]").val("yes");

, / , "" "". Javascript:

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

, !

0

Source: https://habr.com/ru/post/1568903/


All Articles