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
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
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 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
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 am using the following code to show flash message, but it is not working.
PostController
public function store(Request $request){
$this->validate($request,array(
'title'=>'required|max:255',
'slug' =>'required|alpha_dash|min:5|max:255|unique:posts,slug',
'body' =>'required'
));
//store in the database
$post = new Post;
$post->title = $request->title;
$post->slug = $request->slug;
$post->body = $request->body;
$post->save();
//This code will generate flash message about success or failure about data insert
Session::flash('success', 'Data has been saved successfully!');
//Redirect to another page
return redirect()->route('posts.show', $post->id);
}
Then to display it the following code is used:
message.blade.php
#if(Session::has('success'))
<div class="alert alert-success" role= "alert">
<strong>Successful:</strong>
{{ Session::get('success') }}
</div>
#endif
#if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong>Errors:</strong>
<ul>
#foreach($errors as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</div>
#endif
The code above is not showing any flash message. But when
Session::flash('success', 'Data has been saved successfully!');
is written as:
Session::put('success', 'Data has been saved successfully!');
the flash message is displayed and does not disappear.
The routes.php is :
Route::group(['middleware' => ['web']], function(){
Route::get('auth/login', ['as' =>'login', 'uses'=>
'Auth\AuthController#getLogin']);
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', ['as' => 'logout', 'uses' =>
'Auth\AuthController#getLogout']);
Route::get('auth/register','Auth\AuthController#getRegister');
Route::post('auth/register','Auth\AuthController#postRegister');
Route::get('password/reset/{token?}',
'Auth\PasswordController#showResetForm');
Route::post('password/email',
'Auth\PasswordController#sendResetLinkEmail');
Route::post('password/reset','Auth\PasswordController#reset');
Route::get('contact', 'PagesController#getContact');
Route::get('about', 'PagesController#getAbout');
Route::get('/', 'PagesController#getIndex');
Route::get('reader/{slug}', ['as' => 'reader.single', 'uses' =>
'ReaderController#getSingle'])
->where('slug', '[\w\d\-\_]+');
Route::get('reader', ['as' => 'reader.index', 'uses' =>
'ReaderController#getIndex' ]);
Route::resource('posts', 'PostController');
});
Help please!
1.- Make sure if u are including the message template on your view, in this case your SHOW view
2.- Try replacing your message template for this:
#if(Session::has('success'))
<div class="alert alert-success" role= "alert">
<strong>Successful:</strong>
{!! session('success') !!}
</div>
#endif
#if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong>Errors:</strong>
<ul>
#foreach($errors as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</div>
#endif
Look, i'm using {!! session('success') !!} instead of yours