I am learning Laravel. I am doing Form Validation at the moment. In the documentation it is said the variable $errors is flashed to the session and is always available. I get an exception because an undefined variable. I only pasted the sample code from the documentation:
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Error message:
ErrorException in 318c473e4384f7c25db0019a770ee937b30041d1.php line 41: Undefined variable: errors (View: C:\xampp\htdocs\NightClubs\resources\views\add.blade.php)
This are the validation rules in the controller:
$this->validate($request, [
'youtube' => 'required|url',
'coordinatex' => 'required|between:-180,180',
'coordinatey' => 'required|between:-90,90',
'nameofclub' => 'required'
]);
Try to use the group for routes where you want to use $errors variable:
Route::group(['middleware' => ['web']], function () {
// Your routes
// Your routes
}
I assume you have validation rules in your controller.
Try this in your view
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Related
I'm trying to access the $errors variable to my view but it returns an error. Please see my code below.
Controller
$validator = Validator::make($request->all(), [...]);
if($request->required == 1){
$validator = Validator::make($request->all(), [...]);
}
if($validator->fails()){
return Redirect::back()->withErrors($validator)->withInput();
}
View
#if($errors->any())
... Some HTML code here
#endif
Error
Call to a member function any() on string
Any idea? This should work, but it is not.
Reference: https://laravel.com/docs/5.5/validation#quick-displaying-the-validation-errors
Laravel version: 5.5
You need to change your line as per below:
From
return Redirect::back()->withErrors($validator)->withInput();
To
return Redirect::back()->withErrors($validator->errors())->withInput();
Then in you blade file you can access it as:
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
I use validation in view like this below
<div class="{{'form-group required'.$errors->first('name',' has-error')}}">
<label>Title</label>
<input type="text" name="name" class="form-control" required>
<div class="text-danger">{{$errors->has('name') ? $errors->first('name') : ''}}</div>
</div>
I have a form where I upload a file.
In the controller I do all kinds of checks on columns in this file and get some errors.
I add these errors to an array and I want to display all these errors in the view.
I tried all kinds of solution but nothing works.
Right now, I'm doing this in the controller for each line in the file:
$errors[] = array('file_name'=>$file_name, 'error'=>'Invalid coffee name');
And in the view I try these two things:
#if ($errors->any())
{{ implode('', $errors->all('<div>:message</div>')) }}
#endif
#if ($errors->any())
#foreach ($errors->all() as $error)
<div>{{$error}}</div>
#endforeach
#endif
The problem is, although I have 2 errors in the errors array (I checked), I only see the last one in the view.
What am I doing wrong?
I solved it like this:
In my controller method I added:
$errors = new MessageBag();
When I have an error:
$errors->add('coffee', $file_name . ': Invalid coffee name');
In the view:
#if ($errors->any())
<div class="alert alert-danger">
<p>There are errors in the file you uploaded</p>
<ul>
#foreach ($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
I need validate and generate error message if same user try to insert existing project_name to the table in Laravel 5.2. My project table like this
user_id project_name
1 abc
2 sdf
3 kju
My project data store controller as follow
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:3'
]);
$project = new Project;
$project->project_name = $request->input('name');
$project->user_id = Auth::user()->id;
$project->save();
return redirect()->route('projects.index')->with('info','Your Project has been created successfully');
}
and I have alert.blade.php file as
#if ( session()->has('info'))
<div class="alert alert-info" role-"alert">
{{ session()->get('info') }}
</div>
#endif
#if ( session()->has('warning'))
<div class="alert alert-danger" role-"alert">
{{ session()->get('warning') }}
</div>
#endif
how can I do this?
If your form validation is failed then a 422(Unprocessable) response is returned by laravel. And an $error variable will be available in the response. So you can check if the variable is empty or not, and you can display the errors.
Like Below code. This is from laravel 5.2 documentation.
<!-- /resources/views/post/create.blade.php -->
<h1>Create Post</h1>
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<!-- Create Post Form -->
https://laravel.com/docs/5.2/validation
You could foreach the variable $errors if you want catch the error message,
or if you are asking the validation rule, you could use 'exists:database'
https://laravel.com/docs/5.2/validation#rule-exists
I had an issue with occasional TokenMismatchExceptions so I decided to to add
if($e instanceof TokenMismatchException)
{
return \Redirect::to('auth/login')->withErrors(['Seems like you may have waited to long to use this application. Please refresh and try again.']);
}
to the app/Exceptions/Handler.php file in the render function.
I've try a number of different ways to redirect and the view never shows the errors. The view will show errors if I have incorrect login information though.
If I kill the script and dump the session I can see the errors but if I dump the session in the view the errors object is empty.
View
#if (isset($errors) && count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Route
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
/**
* Need to be logged in to access all of these routes.
*/
Route::group(['middleware' => 'auth'], function(){
Route::get('/', function () {
return Redirect::to('/home');
});
});
Laravel 5.1
Try this:
return redirect('auth/login')->with('errors', ['Seems like you may have waited to long to use this application. Please refresh and try again.']);
And then in your view:
#if (session('errors'))
<div class="alert alert-danger">
<ul>
#foreach (session('errors') as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
I am trying to Get Validation Errors on index.blade.php having issues:
When I fill both the fields then it goes well if i just put an Echo or Return in getLogin Controller.
When I just fill one field and it works good if i just put and echo or Return but not giving validation errors, with Validation Errors it only shows, "Something went Wrong"
Code for index.blade.php
<section class="mainWrap">
<div class="headingfirst"><img src="{{ URL::asset('css/des.png') }}" width="78"></div>
<div class="sedhead">Hey User!!!! Try to Login</div>
<div class="againtext">Sign In To Your Account</div>
<article class="FormContainer">
#foreach($errors as $error)
<div class="errors">{{ $error }} </div>
#endforeach
<img class="profile-img" src="{{ URL::asset('css/avatar_2x.png')}}">
{{ Form::open(array('class'=> 'SetMe')) }}
{{ Form::text('email',null, array('placeholder'=>'Email','class'=>'insi')) }}
{{ Form::password('password',array('placeholder'=>'Password','class'=>'dnsi')) }}
{{ Form::submit('Sign In', array('class'=>'SignIn')) }}
{{ Form::close() }}
</article>
</section>
Code for AuthController.php
<?php
class AuthController extends Controller{
public function GetLogin() {
return View::make('layouts.index');
}
public function LogInfo() {
$rules = array('email' => 'required','password' =>'required');
$validator = Validator::make(Input::all(),$rules);
if($validator->fails()){
return Redirect::route('login')
->withErrors($validator);
}
else{
}
}
}
Code for Routes.php
Route::get('login', array('uses'=>'AuthController#GetLogin' ));
Route::post('login', array('uses'=>'AuthController#LogInfo'));
even when i put the Auth Code it don't show anything except "Something goes wrong". but while working with just Echos it works properly
In validation failed statement,
You need to use return Redirect::to('login') instead of return Redirect::route('login').
In index.blade.php, it should be like -
#foreach($errors->all() as $error)
<div class="errors">{{ $error }} </div>
#endforeach
instead of
#foreach($errors as $error)
<div class="errors">{{ $error }} </div>
#endforeach
Also here is my suggestion. if you are currently developing an application using laravel, it is the best to enable debug. Open laravel/app/config/app.php and make sure 'debug' => 'true'. It will help you see what is detailed error messages with stack traces.