问题
I try use this code:
@foreach($errors->all() as $error)
<li>{!! $error !!}</li>
@endforeach
and in view have this:
I would like the error to be displayed correctly. exemple The name is required.
In debbuger I can see. How to replace validation.required upon "The name is required"
回答1:
you need to add validation in controller method like this..
$request->validate([
'name' => 'required'
]);
then you can show your validation error in view :
@if ($errors->has('name'))
<li>{{ $errors->first('name') }}</li>
@endif
and this should also work ...
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
回答2:
Here is a good example of handling errors in laravel
message.blade.php
@if($errors->any())
<div class="alert alert-danger">
<p><strong>Opps Something went wrong</strong></p>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if(session('success'))
<div class="alert alert-success">{{session('success')}}</div>
@endif
@if(session('error'))
<div class="alert alert-danger">{{session('error')}}</div>
@endif
In your controller update method for instance,
public function update(Request $request, $id)
{
$this->validate($request,[
'title'=>'required',
'body'=>'required'
]);
//the above validation is important to get the errors caught
$post= Post::find($id);
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/posts')->with('success','Updated successfully');
}
if you have a layout file as layout.blade.php NOTE: having the error display in the layout file is advantageous to use the message for all purpose.
...
<div class="container">
@include('message')
@yield('content')
</div>
...
回答3:
Ok, I found mistake. I didn't have another language to upload. In file app/config.php replace 'locale' => 'pl' to 'en'
来源:https://stackoverflow.com/questions/51042425/laravel-display-validation-error