Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel back using {{ URL::previous() }} doesn't work if there are validation errors

Tags:

laravel

I want to make a cancel button on a simple CRUD edit form. However it seems that when there are validation errors the cancel button does not work.

I guess because it routes from controller@edit to controller@update the button just goes to controller@edit again instead of the actual previous page. Is this normal or did I do something wrong? How do I make it work?

attendee/edit.blade.php

<div class=pull-right>
{{Form::submit('Update',array('class'=>'btn btn-success'))}}
<a href = "{{URL::previous()}}" class = 'btn btn-warning'>Cancel</a>
</div>
{{ Form::close() }}

routes.php

Route::get('user/attendees', 'UserController@getAttendees');
Route::resource('attendee', 'AttendeeController');

attendee controller

public function edit($id)
{
    $user = Auth::user();
    if(empty($user->id))
        return Redirect::to('/');

    //return if attendee doesn't belong to user
    if( !($user->attendee->contains($id)) )
        return Redirect::to('user/index')->with( 'error', 'attendee id error.' );

    $attendee = Attendee::find($id);
    return View::make('attendees.edit', compact('attendee'));
}

/**
 * Update the specified attendee in storage.
 *
 * @param  int  $id
 * @return Response
 */
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()->with('id', $id);
    }

    $attendee->update($data);

    return Redirect::intended();
}

user controller

public function getAttendees()
{
    list($user,$redirect) = $this->user->checkAuthAndRedirect('user');
    if($redirect){return $redirect;}

    // Show the page
    return View::make('site/user/attendees/index', compact('user'));
}
like image 339
Phil Avatar asked Dec 05 '25 13:12

Phil


2 Answers

I think you should add a hidden input, like this

{!! Form::hidden('redirect_to', old('redirect_to', URL::previous())) !!}

and use this value in your link:

<a href="{{ old('redirect_to', URL::previous())}}">cancel</a>
like image 106
Dima Kuzmin Avatar answered Dec 07 '25 13:12

Dima Kuzmin


It's actually working correctly, it's just the usage that is incorrect.

Take the following:

  1. You submit a form at GET /attendee/edit/{id}
  2. The form takes you to POST /attendee/edit/{id}
  3. You fail validation and are redirected to GET /attendee/edit/{id}

You cannot link to a page using a method other than GET. Your previous route was actually POST /attendee/edit/{id} but the link will always be GET.

Instead, nominate a page to be redirected to, I usually use the index for the section.

like image 21
ollieread Avatar answered Dec 07 '25 15:12

ollieread



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!