如何解决输入数组在 Laravel 验证中应至少包含一个值
我遇到了验证问题, 输入数组应至少包含一个值 例如,当我提交表单时,我得到以下输入
array:26 [
"_token" => "GMPKHos9nnC6zw6UGN4nFKVEGd5PoEpDe9lEPETw"
"Customer" => "1"
"Text_From_Date" => "07 Jun 2021"
"From_Date" => "2021-06-07"
"Text_Due_Date" => "16 Jun 2021"
"Due_Date" => "2021-06-16"
"Invoice_Id" => "INV-1"
"Reference" => null
"Branding" => null
"Add_Currency" => null
"Tax_Type" => "ex_tax"
"Item" => array:5 [
0 => null
1 => null
2 => null
3 => null
4 => null
]
"Product_Description" => array:5 [
0 => null
1 => null
2 => null
3 => null
4 => null
]
"Quantity" => array:5 [
0 => null
1 => null
2 => null
3 => null
4 => null
]
"Unit_Price" => array:5 [
0 => null
1 => null
2 => null
3 => null
4 => null
]
"Discount" => array:5 [
0 => null
1 => null
2 => null
3 => null
4 => null
]
"Discount_Amount" => array:5 [
0 => null
1 => null
2 => null
3 => null
4 => null
]
"Account" => array:5 [
0 => null
1 => null
2 => null
3 => null
4 => null
]
"Tax_Rate" => array:5 [
0 => "0"
1 => "0"
2 => "0"
3 => "0"
4 => "0"
]
"Tax_Rate_Amount" => array:5 [
0 => null
1 => null
2 => null
3 => null
4 => null
]
"Region" => array:5 [
0 => null
1 => null
2 => null
3 => null
4 => null
]
"Amount" => array:5 [
0 => "0"
1 => "0"
2 => "0"
3 => "0"
4 => "0"
]
"Sub_Total" => "0"
"Total_Discount" => "0"
"Grand_Total" => "0"
"Form_Type" => "1"
]
这里是控制器中的验证规则
public function save(Request $request)
{
$rules = [
'Customer' => 'required','Text_From_Date' => 'required','From_Date' => 'required|date','Text_Due_Date' => 'required','Due_Date' => 'required|date','Reference' => 'bail|nullable|string|min:3','Add_Currency' => 'bail|nullable','Tax_Type' => 'required','Product_Description.*' => 'exclude_if:Quantity.*,null|exclude_if:Unit_Price.*,null|required|max:1000','Item.*' => 'exclude_if:Quantity.*,null|bail|nullable','Quantity.*' => 'exclude_if:Product_Description.*,null|required|numeric','Unit_Price.*' => 'exclude_if:Product_Description.*,'Account.*' => 'exclude_if:Product_Description.*,'Amount.*' => 'exclude_if:Product_Description.*,'Sub_Total' => 'required|numeric','Total_Discount' => 'required|numeric',];
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return response()->json($validator->errors(),422);
} else {
dd($validator->validated());
return response()->json('saved',200);
}
}
数组 Item,Product_Description,Unit_Price,Quantity,Discount,Discount_Amount,Account,Tax_Rate,Tax_Rate_Amount,Region,Amount 应该至少有一个 notNull 值。
请帮助解决问题提前谢谢!
解决方法
您可以使用自定义验证。您可以创建自定义规则以保持代码整洁。这只是一个片段
'Quantity' =>['required',function ($attribute,$value,$fail) {
if (is_array($value)&&!array_filter($value)) {
$fail('The '.$attribute.' is invalid.');
}
},],
例如,这里的数据包含数组中的所有空值,因此抛出错误
Array
(
[data] => Array
(
[0] =>
[1] =>
)
)
验证错误
Illuminate\Support\MessageBag Object
(
[messages:protected] => Array
(
[data] => Array
(
[0] => The data is invalid.
)
)
[format:protected] => :message
)
,
使用自定义验证规则,并制定该规则:
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class NotEmptyArrayRule implements Rule
{
private string $key;
private string $value;
public function passes($attribute,$value): bool
{
// remove null items from collection.
$arr = collect($value)->filter();
return $arr->isNotEmpty();
}
public function message(): string
{
return 'Input array is empty.';
}
}
然后使用规则:
'Item' => [
'exclude_if:Quantity.*,null','exclude_if:Unit_Price.*,'bail','nullable',new NotEmptyArrayRule(),]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。