Phil
Phil

Reputation: 2236

Laravel how to pass validation errors back to remote modal window

After validation, I lose the Session/messagebag $error data in my modal. I guess this is because I am loading my edit form attendee/edit into the modal body from a link so the $error data gets sent to the page that calls up the modal but not the modal body. When I turn off the modal and just have it load the page attendee/edit directly, the validation errors show up fine. Is there a way to pass my $error variable to my edit form displayed in the modal body?

I would prefer using AJAX because it seems in my current implementation I need to reload the modal so it pops out/in. But I am not sure how to transfer the $error messagebag object from the edit form/Session data to the modal that way either. I am wondering if there is a way to use this method stackoverflow.com/questions/25103743/laravel-4-validation-in-bootstrap-modal to fill in the $error messagebag.

Main View

    <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();
                }
            }]
        });
    });
});

attendee/edit view

{{ Form::model($attendee, array('class'=>'form-horizontal', 'method' => 'PATCH', 'route' => array('attendee.update', $attendee->id))) }}
        <div class="form-group {{{ $errors->has('first_name') ? 'has-error' : '' }}}">
            <label class="col-xs-3 control-label", for="first_name">First Name</label>
            <div class="col-xs-9">
                {{ Form::text('first_name', null , array('class' => 'form-control')) }}
            </div>
          {{ $errors->first('first_name', '<span class="help-inline">:message</span>')}}
        </div>

      <div class="form-group {{{ $errors->has('special_care') ? 'has-error' : '' }}}">
            <label class="col-xs-3 control-label", for="special_care">Special Care</label>
            <div class="col-xs-9">
                {{ Form::text('special_care', null , array('class' => 'form-control')) }}
            </div>
            {{ $errors->first('age', '<span class="help-inline">:message</span>')}}
        </div>
        {{Form::submit()}}
{{ Form::close() }}

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 the $errors do not get passed to the modal.

Upvotes: 0

Views: 2234

Answers (1)

Faruk Omar
Faruk Omar

Reputation: 1193

Named Error Bags

If you have multiple forms on a single page, you may wish to name the MessageBag of errors. This will allow you to retrieve the error messages for a specific form. Simply pass a name as the second argument to withErrors:

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

You may then access the named MessageBag instance from the $errors variable:

<?php echo $errors->update->first('fieldName'); ?>

Upvotes: 1

Related Questions