How to Redirect Back to Form with Input - Laravel 5

How to redirect back to form with input - Laravel 5

You can use the following:

return Redirect::back()->withInput(Input::all());

If you're using Form Request Validation, this is exactly how Laravel will redirect you back with errors and the given input.

Excerpt from \Illuminate\Foundation\Validation\ValidatesRequests:

return redirect()->to($this->getRedirectUrl())
->withInput($request->input())
->withErrors($errors, $this->errorBag());

Redirect back with input not working in laravel 5.5

My approach would be using FormRequest.

php artisan make:request VerifyCodeRequest

And body of the request:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class VerifyCodeRequest extends FormRequest
{

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'verification_code' => 'required|in:'.$this->user()->verification_code, // since user is already logged in we check if verification_code matches using `in` rule
];
}

public function messages()
{
return [
'in' => 'Your custom error message if we failed',
];
}
}

Now you change signature of verifyMobileCode(...) to the following verifyMobileCode(App\Http\Requests\VerifyCodeRequest $request).

Now, code executes body of the function only if we pass the validation so we need to update user instance and we are done.

public function verifyMobileCode(App\Http\Requests\VerifyCodeRequest $request)
{
$request->user()->verify();
return redirect('/')->with('success', 'Account verified.');
}

Inside User model add verify() function

public function verify() 
{
$this->update(['verified' => true]);
}

If your above approach does not work the there is some issue with your app/Http/Kernel.php. You have used

\Illuminate\Session\Middleware\StartSession::class,

twice. You must delete one. Probably the first one.

See the stock Laravel's kernel.php.

Redirect back with 'select' values in Laravel 5

If you are iterating in a loop then directly old will not work for select option

For example if you have something like that in your blade file
then you can try this for selecting the current option after redirect

<select id="someId" name="someName">

@foreach($someData as $data)
<option value="{{ $data->id }}" @if(old('someName') == $data->id) selected @endif> {{ $data->name }} </option>
@endforeach

</select>

Then it will select your current option.

If this is your problem then it might help.

Redirect back with input Laravel

You need to use old for all elements you need input, see example below:

<form action="{{ route('signin') }}" method="post">
<div class="form-group {{$errors->has('in_email') ? 'has-error' : ''}}">
<label for="email">Your E-Mail</label>
<input class="form-control" type="text" name="in_email" id="email" value="{{ old('in_email') }}">
@if ($errors->has('in_email'))
<span class="help-block">{{$errors->first('in_email')}}</span>
@endif
</div>

<div class="form-group {{$errors->has('in_password') ? 'has-error' : ''}}">
<label for="password">Your Password</label>
<input class="form-control" type="password" name="in_password" id="password" value="{{ old('in_password') }}">
@if ($errors->has('in_password'))
<span class="help-block">{{$errors->first('in_password')}}</span>
@endif
</div>
{{ csrf_field() }}
<button type="submit" class="btn btn-primary">Submit</button>
</form>


Related Topics



Leave a reply



Submit