php - Laravel如何在错误填写表单字段时引发单个错误

标签 php laravel error-handling

通常,当我创建表单时,在数据输入错误的字段下会显示错误。 enter image description here

代码看起来像这样

<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                    <label for="name" class="control-label">Imie</label>
                    <div class="col-md6">
                        <input id="name" maxlength="255" type="text" class="form-control" name="name" required autofocus>
                        @if ($errors->has('name'))
                            <span class='help-block'>
                                <strong>{{ $errors->first('name') }}</strong>
                            </span>
                        @endif
                    </div>
                </div>

我有一个来自客户的规范,不告诉用户错误填写了哪个字段,我想知道如何使它看起来像这样
enter image description here

最佳答案

我建议删除单输入中的所有错误消息,并像这样仅在顶部放置一个:

@if (!empty($errors))
    <span class='help-block'>
        <strong>{{ "Some input field is not properly filled" }}</strong>
    </span>
@endif

并且所有输入字段都应该是这样的:
<div class="form-group">
    <label for="name" class="control-label">Imie</label>
    <div class="col-md6">
        <input id="name" maxlength="255" type="text" class="form-control" name="name" required autofocus>
    </div>
</div>

现在,如果您在任何字段中都有错误,那么您将获得该消息的顶部。

关于php - Laravel如何在错误填写表单字段时引发单个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44613725/

相关文章:

javascript - 验证 JS 代码的 API/库/工具?

php - Laravel:区别 App::bind 和 App::singleton

php - 如何使用两个表laravel从同一数据库中获取数据

error-handling - 如何防止 Apollo 客户端 useQuery 中的错误传播?

php - 在 Laravel 中显示消息用户

php - 请参阅带有参数的学说查询构建器 DQL

r - 禁止coxph中的特定警告会导致cox.zph错误

javascript - 如何处理webview错误并清除警告?

sql - 测试后 Laravel 重置数据库

php - 使用Homestead时检索客户端凭据授予 token