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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With