gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:39:38 26 4
gpt4 key购买 nike

我正在向我的 Controller 提交一组输入,如下所示:

<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">

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

$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();
}

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

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

最佳答案

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

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

{{ $errors->first('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

其他示例:

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

来自 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) {
//
}

希望对你有帮助!

关于php - 在 Laravel 中显示输入数组的验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28333809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com