如何在Laravel 5中每个相关输入字段旁边显示验证错误?

问题描述:

默认解决方案很简单:

@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

,我可以在任何地方包含 errors.blade.php .

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?

我认为这将要求我在每个输入旁边定义很多条件 if 语句,对吗?

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?

谢谢.

您可以使用类似这样的东西:

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>