Failed to access validation errors.

I installed the controller with some verification.

public function attemptLogin() { $rules = array( 'email'=>'required|email', 'password'=>'required' ); $validator = Validator::make(Input::all() , $rules); if($validator->fails()){ return Redirect::to('login')->withErrors($validator); }; } 

If I output messages directly to the controller

 $messages = $validator->messages(); print_R($messages->all()); 

I get validation errors - however, if I redirect:

 return Redirect::to('login')->withErrors($validator); 

The $errors array available in the view is always empty.

+6
source share
5 answers

From laravel 4 documentation

Note that when validation fails, we pass the Validator instance to the Redirect using the withErrors method. This method flashes the error messages per session so that they are available on the next request.

The $errors variable is not an array.

The $errors variable will be an instance of MessageBag .

For some reason, I don't really like the @seamlss idea. Instead, you can use this .

 @if(Session::has('errors')) <? $errors = Session::get('errors'); ?> <div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">&times;</button> <ul> <li id="form-errors" > <h3> {{ $errors->first('email') }}</h3> </li> </ul> </div> @endif 

I used some bootstrap components, do not get confused, the only thing you need is lines with curly braces and a magic @ sign.

From laravel docs error-messages-and-views

So, it is important to note that the $ errors variable will always be available in all your views, for each request, which makes it convenient to assume that the $ errors variable is always defined and can be used safely.

You can also check this.

 @if( $errors->has('email') ) <div class="control-group error"> <label class="control-label" for="email">Email</label> <div class="controls"> <input type="text" id="email" placeholder="Email" name="email"> <span class="help-inline">{{ $errors->first('email') }}</span> </div> </div> @endif 
+8
source

I had a successful redirect using ->withErrors($validation)

And then in the view checking the @if( $errors->count() > 0 ) condition @if( $errors->count() > 0 ) ...

 @if( $errors->count() > 0 ) <p>The following errors have occurred:</p> <ul id="form-errors"> {{ $errors->first('username', '<li>:message</li>') }} {{ $errors->first('password', '<li>:message</li>') }} {{ $errors->first('password_confirmation', '<li>:message</li>') }} </ul> @endif 
+5
source

You tried:

 return Redirect::to('login')->withErrors($validator); 

or

 return Redirect::to('login')->withErrors($validator->messages()); 
0
source

Here is another possibility when you have difficulty displaying errors and when they are correctly passed through ->withErrors() . If you are redirected to a controller action that should perform some checks of its own state and calls ->withErrors() , it will obscure the $errors that were entered first.

0
source

Try to execute

 return Redirect::to('login')->with('errors',$validator->errors->all()); 

then from the browse cycle through these errors

 foreach($errors as $error) { print $error; } 
-1
source

All Articles