Tautvydas
Tautvydas

Reputation: 102

Laravel - requests validation loose errors on redirect

I have a custom request file in http/requests which handles validation.

If validation does not pass, by default it should redirect back with $errors in the view (which are generated by \Illuminate\View\Middleware\ShareErrorsFromSession::class) and I see that the errors are in the session while debuging, but after this request with code 302 finishes and 200 request is generated, errors in the session are lost.

What might cause this issue? Thanks

Code:

<?php

namespace App\Http\Requests;
<..>
class AnswerQuestionnaireRequest extends FormRequest
{
    private $questionRepository;

    /**
     * AnswerQuestionnaireRequest constructor.
     */
    public function __construct(QuestionRepository $qs)
    {
        parent::__construct();
        $this->questionRepository = $qs;
    }

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        $rules = [];
        foreach ($this->questionRepository->getAll() as $question){
            $index = "question-" . $question->id;
            $rules[$index] = "required|in:1,2";
        }

        return $rules;

    }

    public function messages()
    {
        return [
            'question-1.required' => __("Incorrect value"),
            'question-2.required'  => __("Incorrect value"),
        ];
    }

}


class QuestionnairesController extends Controller
{

    public function __construct(QuestionnairesService $questionnairesService, QuestionRepository $questionRepository)
    {
        $this->questionnairesService = $questionnairesService;
        $this->questionRepository = $questionRepository;
    }

    public function show(Request $request){
        $questions = $this->questionRepository->getAll();
        return view("client/questionnaire", compact("questions"));
    }

    public function store(AnswerQuestionnaireRequest $request){
        var_dump($request);
    }

EDIT: Checking with Xdebug you can see, that the validation worked, but it's only for the request, that return code 302 and redirects back. https://gyazo.com/231c83910f6e57748e1b705ade74e383 And when the request 200 is loading, this error bag is already empty there.

Upvotes: 3

Views: 1012

Answers (2)

Tautvydas
Tautvydas

Reputation: 102

Wrapping everything in routes/web.php to "Route::group(['middleware' => ['web']], function () {" fixed the issue.

I thought that everything in web.php file is already assigned to "WEB" middleware...

Upvotes: 0

spartyboy
spartyboy

Reputation: 426

In your controller try this, assuming the function is create

use Illuminate\Http\Request;
public function create(Request $request){
    $this->validate($request,[
    'field'=>'required|present',
    'another_field'=>'required|present',
    ]);
   Post::create($request);
}

Do note that the $this->validate(), if there is a validation error it is automatically redirected back to the previous url with the error messages sent. Blade You can then check for errors this way

@if($errors->has('fieldName'))
    <small class="text-danger form-text">{{$errors->first('fieldName')}}</small>
@endif

Upvotes: 1

Related Questions