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
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 tried following code: if the user's info to login is not correct redirect the user to the loin page again with the a message:
if (!$info_is_correct){
$_SESSION['err_msg'] = 'your login info is not correct';
return redirect('http://localhost:8000/user/login');
}
this is what I do in the login page(view) to echo the error msg:
#isset($_SESSION['err_msg'])
<div class="alert alert-danger mt-2" style="font-family:IRANSans;font-size:16px ;">
<p>
<strong>خطا !</strong>
{{$_SESSION['err_msg']}}
<?php unset($_SESSION['err_msg']) ?>
</p>
</div>
#endisset
where is the problem? and What is the best way to send data to view with redirecting?
Use the with() method:
return redirect('http://localhost:8000/user/login')->with('err_msg', 'your login info is not correct');
And if a view:
#if (session('err_msg'))
<div>{{ session('err_msg') }}</div>
#endif
You can do like this to send your error messages with use inputs
return redirect('http://localhost:8000/user/login')->with('err_msg', 'Your error message')->withInput();
Source : Laravel Redirects, Laravel Validate
Note : You can also work with Error Messages like this
Since you are using laravel the validations can be done by making use of validators and simply return
redirect()->back()->withErrors($validator)
and from the view you can have access to an $errors variable which will hold all the errors,
and you can loop through errors variable and print out the errors
from your login view
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Set session value in laravel with session method, be sure to use because it is global and set session value for whole site.
Also remove http://localhost:8000 to work this route in both development and production.
if (!$info_is_correct){
session('err_msg', 'your login info is not correct');
return redirect('/user/login');
}
And get value form session as session('key')
{{session('err_msg')}}
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 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
in controller i'm using:
if ($validator->fails())
{
return Redirect::to('/admin/profile')
->withErrors($validator)
->withInput();
}
how to get result of withErrors in view ?
{{ $errors->all() }}
If you want to show all errors in one place you can use
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
If you want to show each field's errors e.g. bellow the field you can use (e.g. for email)
{{ $errors->first('email') }}
we use first() in order to show only one error each time for each field.