validation message not showing in laravel 5.3 - validation

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

Related

How to show only error message in Laravel Validator

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

Laravel request validation doesn't show error messages

After I used group middleware, I am not able to access error messages. Error bags returns empty.
There was no problem before.
I have researched, some users have solved the problem by changing http/kernel.php
\Illuminate\Session\Middleware\StartSession::class, $middlewareGroups to $middleware.
However, It doesn't work for me.
Also $validated = $request->validated(); function doesnt returns validation error. In my CreditcardRequest Class I have attributes, messages, rules functions. If validation fails these messages needs to be shown.
previously When validated(); method was running on the controller, it was showing the messages if the form is empty. I have 20 pages all of them working, before middleware grouping.
Creditcard Blade
<div class="messages">
#if ($errors->any())
<div class="row mt-3">
<div class="col-md-12">
<div class="alert alert-warning alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h3 class="alert-heading font-size-h4 font-w400">Error!</h3>
#foreach ($errors->all() as $error)
<p class="mb-0">{{ $error }}</p>
#endforeach
</div>
</div>
</div>
#endif
</div>
CreditcardRequest
public function attributes()
{
return [
'cc_name' => 'CC Owner',
..
];
}
public function messages()
{
return [
'required' => 'Required: :attribute',
...
];
}
public function rules()
{
return [
'cc_name' => 'required|max:128',
];
}
Controller
public function doPaySection(CreditcardRequest $request)
{
$validated = $request->validated();
$cc = TRUE;
if ($cc):
return redirect('/pay_success')->with('success', 'success');
else:
return redirect('/pay_error')->with('error', 'error');
endif;
}
web.php
Route::group(['middleware' => ['client.role:guest']], function () {
Route::get('/login', 'HomepageController#showLogin')->name('login');
Route::post('/login', 'HomepageController#doLogin');
Route::post('/register', 'HomepageController#doRegister');
Route::get('/register', 'HomepageController#showRegister')->name('register');
});
login.blade
#if ($errors->any())
<div class="row mt-3">
<div class="col-md-12">
<div class="alert alert-warning alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h3 class="alert-heading font-size-h4 font-w400">Hata!</h3>
#foreach ($errors->all() as $error)
<p class="mb-0">{{ $error }}</p>
#endforeach
</div>
</div>
</div>
#endif
Controller
public function doLogin(Request $request)
{
if (auth()->guard('client')->attempt(['email' => request('email'), 'password' => request('password')])) {
return redirect()->intended('/');
} else {
return redirect()->back()->with('error', 'error');
}
}
Can you try using this header in your request. Especially if you are hitting from postman.
Accept:application/json
Before using this, i was getting csrf token in case of invalid requests.
The code you have at the minute won't add a message to the $errors MessageBag, it will simply add a value to the session called error.
If you want to add an error to the message bag you could simply throw a ValidationException which redirect back with that message:
public function doLogin(Request $request)
{
if (auth()->guard('client')->attempt($request->only('email', 'password'))) {
return redirect()->intended('/');
}
throw ValidationException::withMessages([
'error' => 'The error message',
]);
}
Don't forget to import ValidationException with:
use Illuminate\Validation\ValidationException;
Your will be able to get in session('error')from below
return redirect()->back()->with('errors', 'error');
So your code would be like
#if (session('errors'))
<div class="row mt-3">
<div class="col-md-12">
<div class="alert alert-warning alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h3 class="alert-heading font-size-h4 font-w400">Hata!</h3>
#foreach (session('errors') as $error)
<p class="mb-0">{{ $error }}</p>
#endforeach
</div>
</div>
</div>
#endif

laravel Form Validation errors

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

laravel5.2 errors not show up in blade

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

Why does not work withErrors in Laravel?

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

Resources