I am trying solve this wired problem for hours, but still the errors message won't show up in template.
I have make sure the web middleware (which contains StartSession and ShareErrorsFromSession) is wrapped all my route.
Route::group(['namespace'=>'admin','prefix'=>'admin','middleware'=>['web']],function (){
Route::get('index','AdminController#index');
Route::get('addCase','CaseController#create');
Route::any('upload','AdminController#upload');
Route::resource('case', 'CaseController');
});
public function store(Request $resquest)
{
//
$validator = Validator::make($resquest->all(), [
'title' => 'required',
]);
if($validator->passes()){
}else{
return back()->withErrors($validator);
}
}
<form method="post" class="form-horizontal" action="{{action('admin\CaseController#store')}}">
{{ csrf_field() }}
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<button>submit</button>
</form>
please help!
the validation is worked, but the errors vanished
Please try this
In controller
$validator = Validator::make($request->all(),[
'title' => 'required'});
if($validator->fails()) {
return Redirect::back()->withErrors($validator);
}
In view page
#if ($errors->any())
<ul class="alert alert-danger">
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif
Related
I want only error messages not the field with codes
Controller
$validator = Validator::make(request()->all(),[
'txtName' =>'required|max:25',
]);
if($validator->fails()) {
$errors = $validator->errors();
return redirect()->back()->with('error', $errors);
}
View:
#if(session()->has('error'))
{!! \Session::get('error') !!}
#endif
Return:
{"txtName":["The txt name may not be greater than 25 characters."]}
What I need:
name may not be greater than 25 characters
First instead of with use withErrors
if($validator->fails()) {
$errors = $validator->errors();
return redirect()->back()->withErrors($errors);
}
and in view
#if($errors->any())
<div class="alert alert-danger">
<ul class="list-unstyled">
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Use #foreach.
#if(session()->has('error'))
#foreach($errors->all() as $error as $error)
<p>{{ $error }}</p>
#endforeach
#endif
When I flash one particular message it shows on localhost but not on production server. What can be cause of this problem.All other messages are shown but not this one
//Blade Template
#if (count($errors) > 0)
<div class="alert alert-danger">
×
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#foreach (['danger', 'warning', 'success', 'info'] as $msg)
#if(session('alert-' . $msg))
<div class="alert alert-{{ $msg }}">
{{ session('alert-' . $msg) }} ×
</div>
#endif
#endforeach
//Controller Code
return redirect('url')->with('alert-danger','All lines are already reserved, Please try again later.');
``
Also it does show up when I dd() the redirect object, see attached image
[1]: https://i.stack.imgur.com/C9ZXZ.png
Try flashing manually, because redirect return a 301 HTTP response, not a view
/*request()->*/session()->flash('alert-danger', 'All lines are already reserved, Please try again later.');
return redirect('url');
version: laravel 5.7
Router:
Route::get('regist','User\RegistController#registView');
Route::post('regist','User\RegistController#regist');
Form:
<form class="form-signin" method="POST" action="/regist">
.....
</form>
Validate:
$this->validator=Validator::make($input,$rule,$message);
if($this->validator->fails()){
return \Redirect::back()->withErrors($this->err());
}
The Problem:
Do not display an error message.Need to press Enter in the address bar to reload the page.Want to use the Validateor::make method.How can I modify it?
If the validation worked, did your put the error message to be displayed on the page you redirected to?
for example in your blade page did you do something like below?
#if(count($errors->all()) > 0)
<div class="alert alert-danger" role="alert">
<p><b>Required Fields Missing!</b></p>
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
I will show you a simple example, if you are in the controller use the validator facade
use Illuminate\Support\Facades\Validator;
In the conroller user the below code
public function update(Request $request, $id) //use of request
{
$this->validate($request, [
'name' => 'required',
], [
'name.required' => 'Name is required',
]);
}
If validation fails it will automatically redirect back or you could use a validator fail statement and send a modified message.
After that you should put an if statement to display the error message as below:
#if(count($errors->all()) > 0)
<div class="alert alert-danger" role="alert">
<p><b>Required Fields Missing!</b></p>
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Or if you have only one defined error message use:
#if(Session::has('success'))
<div class="alert alert-success" role="alert">
{ Session::get('success') }}
</div>
#endif
Hope this helps
For add validation on form you can use Laravel Request Validation classes. which will help you to pull validation in separate class.
Here is the reference for request classes :- https://laravel.com/docs/5.7/validation#form-request-validation
That will help you to pull validation and also improvements in your code.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
I am using validation in laravel 5.3. but error message not getting displayed. what to do?
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name'=>'required|min:2',
'address'=>'required',
'email'=>'required',
'contact_number'=>'required',
'date_of_birth'=>'required',
'company_name'=>'required',
'country'=>'required',
'city'=>'required',
'fax'=>'required',
'telephone'=>'required',
'picture_upload'=>'required',
]);
}
in view
#foreach ($errors->all() as $error)
<li>{!! $error !!}</li>
#endforeach
I usually use FromRequests for validation, but I'm pretty sure that the validator takes the Request object, but you are passing it an array $request->all(), simply change that to: $request
Do like this :
In Controller
$this->validate($request, [
'name'=>'required|min:2',
'address'=>'required',
'email'=>'required',
'contact_number'=>'required',
'date_of_birth'=>'required',
'company_name'=>'required',
'country'=>'required',
'city'=>'required',
'fax'=>'required',
'telephone'=>'required',
'picture_upload'=>'required',
]);
In View
#if (count($errors) > 0)
<div class="alert alert-danger alert-dismissible fade in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<strong>OOPS! You might have missed to fill some required fields. Please check the errors. <strong>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
I use withErrors() to pass validation messages in template blade:
if ($validator->fails()) {
dd($validator); // Gives me filled array with messages
return Redirect::back()
->withErrors($validator)
->withInput();
In template I have:
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
I am guess that problem in the call stack of templates blade, or in function withErrors.
If withErrors uses session, maybe this is one of problem.
Additionally this is my call validation:
$validator = Validator::make($request->all(), [
"name" => 'required|string|min:10',
"text" => 'required|string|min:10',
]);
Try this in view:
#if(Session::has('error'))
{{ Session::get('error') }}
#endif