Skip to content
Advertisement

How to Display Validation Errors Next to Each Related Input Field in Laravel 5?

Default solution is trivial:

@if (count($errors) > 0)
<ul id="login-validation-errors" class="validation-errors">
    @foreach ($errors->all() as $error)
    <li class="validation-error-item">{{ $error }}</li>
    @endforeach
</ul>
@endif

and I can include errors.blade.php anywhere.

Is there any way to extract each element and display it next to input field that holds the value that failed?

I assume that would require me to define a lot of conditional if statements next to each input, right?

How to sort this problem? Could you give me any examples?

Thanks.

Advertisement

Answer

You can use something like this :

<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
    <label for="name" class="col-sm-3 control-label">Name: </label>
    <div class="col-sm-6">
        <input class="form-control" required="required" name="name" type="text" id="name">
        {!! $errors->first('name', '<p class="help-block">:message</p>') !!}
    </div>
</div>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement