Laravel 5.2
As I understand the problem is that the session does not save the form data and the text of validation errors. If I do the same thing, but without validating the data in the Controller, then everything is perfectly preserved in the session.
There is a simple view with the form of sending data (through the template 'blade' ):
@if( count($errors) > 0 ) <div class="alert alert-danger"> <ul> @foreach( $errors->all() as $error ) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form method="POST" action="{{ route('contact') }}"> <!-- <?//='/contact');?> Or <?//=route('contact');?> --> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="form-group"> <label for="name">Name:</label> <input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}" placeholder="Enter Name"> </div> <div class="form-group"> <label for="email">Email address:</label> <input type="email" class="form-control" id="email" name="email" value="{{ old('email') }}" placeholder="Enter E-mail"> </div> <div class="form-group"> <label for="site">Site:</label> <input type="text" class="form-control" id="site" name="site" value="{{ old('site') }}" placeholder="Enter Site"> </div> <div class="form-group"> <label for="text_area">Text:</label> <textarea class="form-control" id="text_area" name="text_area" rows="3" placeholder="Some text....."> {{ old('text_area') }} </textarea> </div> <div class="checkbox"> <label><input type="checkbox" name="checkbox"> Remember me</label> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> <!--/class="col-"--> </div> <!--/class="row"--> @endsection There is a simple controller ContactController.php
namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; class ContactController extends Controller { public function show( Request $request, $prm=false ){ $my_array = ['title1'=>'This variable `$title1` content', 'title2'=>'This variable `$title2` content', 'title3'=>'This variable `$title3` content']; //массив $my_array2 = ['one'=>array('param1'=>'This variable `param1` content', 'param2'=>'This variable `param2` content', 'param3'=>'This variable `param3` content'), 'two'=>array('param4'=>'This variabl e `param4` content', 'param5'=>'This variable `param5` content', 'param6'=>'This variable `param6` content') ]; $my_array3 = array( 'title'=>'Contact', 'data'=>[ 'one'=>'list 1', 'two'=>'list 2', 'three'=>'list 3', 'four'=>'list 4', 'five'=>'list 5', ], 'dataI'=>['list-1','list-2','list-3','list-4','list-6','list-6'], 'bvar'=>true, 'script'=>"<script>alert('Hello! ++')</script>" ); /** VALIDATION on Request */ if( $request->isMethod('post') ) { //$request->session()->reflash(); //$request->session()->keep(['name', 'email', 'site', 'text_area']); //$request->session()->reflash(); //dump( $request->session()->all() ); $rules = [ 'name' => 'required|max:10', 'email' => 'required|email', //'site'=>'required', //'text_area'=>'required', ]; $messages = [ 'required' => 'The :attribute field is required.', ]; $this->validate($request, $rules, $messages); dump( $request->all() ); dump( $request->session()->all() ); } if( view()->exists('default.contact') ){ return view('default.contact') ->withMydata($my_array2) ->withMydata2($my_array) ->withMydata3($my_array3); } else { abort(404); } } } In theory, when data validation according to the established rules is successful for each of the listed Form fields, the code after initializing the validation process ( $ this-> validate line ($ request, $ rules, $ messages); ) and I get a dump order what's in the POST and what will be recorded in the session. When validation is successful, the data in the Form should not remain, but when validation is failed, an exception should be generated and a redirect should occur to the previous page, and the data should be saved in the session and, thanks to the specified construction, in the view value = "{{old (' name ')}} " displayed in the fields. But this is not happening to me. If I enter the data correctly in the form so that the validation is successful, I see a POST printout, which means that the validation did succeed (and the rules are valid, as indicated in the Controller), but when I enter the data that is not valid for validation, I’ll fail its, then the stored data in the fields I do not see that, as I understand it, should be automatically incorporated into this validation mechanism. I will also say that permission to write flash () data , and in general, I also wrote the entry to the session in /app/Http/Kernel.php
/** * The application's global HTTP middleware stack. * * These middleware are run during every request to your application. * * @var array */ protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, ]; At the same time, as I said, if I do not conduct data validation in the controller, and set $ request-> flash (); so that the data from the POST are written to the session once, then they are recorded and remain in the Form, i.e. here everything works correctly. But using validation, I have this line $ request-> flash (); delete because judging by the documentation, the validation mechanism does it itself and it is not necessary to write it. Someone who tells me that I forgot to register or did it wrong, that validation does not work for me in conjunction with recording to the session and displaying after the failure of validation and, accordingly, the redirect of old values to input. And there are no validation errors either, although I also indicated them in the view.
@if( count($errors) > 0 ) <div class="alert alert-danger"> <ul> @foreach( $errors->all() as $error ) <li>{{ $error }}</li> @endforeach </ul> </div> @endif P / S: As I can see, the validation works out and its rules are valid, but I do not see the display of validation errors when it is not passed and the data in the input fields when the Form is filled out during the redirect back.