Reputation: 810
I am validating input using rules in laravel, Currently, I am using the below code to show errors in my view if validation fails.
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
The problem with the above code is all the errors are coming in one place. How can I show errors below their respective inputs (Like Name, Email, Password, etc). And where can I define my custom error messages? Thanks
Upvotes: 4
Views: 25897
Reputation: 1019
You can use like that for every field
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}"
name="name" value="{{ old('name') }}" autofocus>
@if ($errors->has('name'))
<span class="invalid feedback"role="alert">
<strong>{{ $errors->first('name') }}.</strong>
</span>
@endif
</div>
</div>
Upvotes: 0
Reputation: 10210
As of Laravel 5.8.13, you can use the @error
blade directive. Just place your desired error markup below your input field:
@error('field-name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
If there was an error validating field-name
, the message related to the error will be displayed.
As for customising your validation error messages, check the Laravel documentation on Custom Error Messages for further information.
Upvotes: 9