在 Laravel 中显示输入数组的验证错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28333809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:50:17  来源:igfitidea点击:

Displaying validation errors from an input array in Laravel

phpvalidationlaravel

提问by Josh Mountain

I am submitting an array of inputs to my controller like so:

我正在向我的控制器提交输入数组,如下所示:

<input id="box-1-nickname" name="box-nickname[]" class="form-control" type="text" placeholder="Required">
<input id="box-2-nickname" name="box-nickname[]" class="form-control" type="text" placeholder="Required">

I am doing some validation like this:

我正在做一些这样的验证:

$validator = Validator::make(Input::all(), array(
        'supplies-count' => 'required|in:0,1,2,3,4',
    ));

$arrayValidator = Validator::make(Input::all(), []);

$arrayValidator->each('box-nickname', ['required|min:1|max:60']);

if( $validator->fails() || $arrayValidator->fails() ) {
    return Redirect::route('route-2')
           ->withErrors($arrayValidator)
           ->withInput();
}

The problem is when I try to check the errors like this it doesn't work:

问题是当我尝试检查这样的错误时它不起作用:

if( $errors->has('box-1-nickname') ) { echo ' has-error'; }

回答by WebSpanner

You've probably long found a solution, but for anyone else who stumbles across this:

您可能早就找到了解决方案,但对于其他遇到此问题的人:

The validator uses array dot notation of the field array keys. For example box-nickname[0]becomes box-nickname.0

验证器使用字段数组键的数组点表示法。例如box-nickname[0]变成box-nickname.0

Therefore if( $messages->has('box-nickname.0') ) { echo ' has-error'; }should give you your desired result. However, you will need to dynamically generate the array key since as you've said, you won't know how many box-nicknames are being applied. I use this in my form view:

因此if( $messages->has('box-nickname.0') ) { echo ' has-error'; }应该给你你想要的结果。但是,您需要动态生成数组键,因为正如您所说,您不知道应用了多少个框昵称。我在表单视图中使用它:

@if(!is_null(Input::old('box-nickname')))
    @foreach(Input::old('box-nickname') as $n => $box-nickname)
        @include('box-nickname-create-form-partial')
    @endforeach
@endif

Then create a partial view called "box-nickname-create-form-partial.blade.php" or whatever you want to call it with the form field, which might look something like this:

然后创建一个名为“box-nickname-create-form-partial.blade.php”或任何你想用表单字段调用它的部分视图,它可能看起来像这样:

<div class="form-group {!! $errors->has('box-nickname.'.$n) ? ' has-error' : '' !!}">
    <input name="box-nickname[{{$n}}]" class="form-control" type="text" placeholder="Required">
</div>

I hope that's helpful.

我希望这会有所帮助。

回答by Mike

Displaying input array errors in the view (L5.8 onwards)

在视图中显示输入数组错误(L5.8 以上)

To get the first validation error for an input array:

要获取输入数组的第一个验证错误:

{{ $errors->first('input_array.*') }}

To check if there is an error within an input array:

要检查输入数组中是否存在错误:

@if($errors->has('input_array.*'))
    <h1>There is an error in your input array</h1>
    <ul>
       @foreach($errors->get('input_array.*') as $errors)
           @foreach($errors as $error)
               <li>{{ $error }}</li>
           @endforeach
       @endforeach
    </ul>
@endif

Other examples:

其他例子:

@error('input_array.*')
    <div class="alert alert-danger">{{ $message }}</div>
@enderror

From 5.8^ documentation

来自 5.8^ 文档

Working with error messages

处理错误消息

If you are validating an array form field, you may retrieve all of the messages for each of the array elements using the * character:

如果您正在验证数组表单字段,您可以使用 * 字符检索每个数组元素的所有消息:

foreach ($errors->get('attachments.*') as $message) {
    //
}

Hope it helps!

希望能帮助到你!

回答by mopo922

The errors are collected by nameproperty, not id, and Laravel's default MessageBag variable is $messages, not $errors:

错误是由name属性而不是收集的id,而 Laravel 的默认 MessageBag 变量是$messages,而不是$errors

if( $messages->has('box-nickname') ) { echo ' has-error'; }

http://laravel.com/docs/4.2/validation#working-with-error-messages

http://laravel.com/docs/4.2/validation#working-with-error-messages

回答by David Nguyen

$errorsis correct, but you should be checking for box-nickname. As you can see you will run into the issue of not being able to identify what box is what because of the generic name. I think the easiest way to to give each input a unique name (eg. box-1, box-2)and do a for loop on the server side to retrieve inputs that start with box-.

$errors是正确的,但您应该检查box-nickname. 如您所见,您会遇到由于通用名称而无法识别哪个框是什么的问题。我认为最简单的方法是给每个输入一个唯一的名称(例如box-1box-2)并在服务器端执行 for 循环来检索以box-.