无法从 Laravel 表单请求验证中的对象数组中排除空值?
Unable to exclude null values from array of objects in Laravel Form Request Validation?
注意: 将 'details' 存储为数据库中的 json 字段
表单请求验证码:
public function rules(Request $request)
{
return [
'name' => 'required | string',
'details.subscriptions' => 'required | array',
'details.subscriptions.*' => 'nullable | string',
'details.size' => 'nullable | array',
'details.size.*' => 'nullable | string',
'details.notes' => 'nullable | array',
'details.notes.*' => 'nullable | string',
'details.price' => 'nullable | array',
'details.price.*' => 'nullable | double',
];
}
protected function prepareForValidation()
{
$this->merge([
'name' => ucwords($this->name),
]);
}
验证结果 - 我得到的结果:
{
"name":"Some Name",
"details":{
"subscriptions":["1"],
"size":[null,null],
"notes":["10 pcs",null],
"price":[100,null]}
}
我想得到的:
{
"name":"Some Name",
"details":{
"subscriptions":["1"],
"notes":["10 pcs"],
"price":[100]
}
}
请帮忙,我卡住了。
在控制器存储方法中试过这个:
$validated = $request->validated();
$collection = collect($validated);
$size = array_filter($validated['details']['size'], function (
$value
) {
return !is_null($value);
});
这个returns一个空数组。但是我不知道下一步该做什么。
您可以在有 null
值时进行过滤,如果数组现在为空则再次过滤。有多种方法可以实现,这只是一种可能的解决方案。
<?php
$validated = json_decode('{
"name":"Some Name",
"details":{
"subscriptions":["1"],
"size":[null,null],
"notes":["10 pcs",null],
"price":[100,null]}
}', true);
//filter null values
foreach($validated['details'] as $key => $detail) {
$validated['details'][$key] = array_filter($validated['details'][$key], function ($value) {
return !is_null($value);
});
}
//filter empty arrays
$validated['details'] = array_filter($validated['details'], function ($value) {
return !empty($value);
});
print_r($validated);
你可以做一个递归过滤器。
public function index(YourRequest $request)
{
$validated = $request->validated();
$validated = $this->filterRecursive($validated);
dd($validated);
}
public function filterRecursive($values)
{
foreach ($values as &$value) {
if (is_array($value)) {
$value = $this->filterRecursive($value);
}
}
return array_filter($values);
}
注意: 将 'details' 存储为数据库中的 json 字段
表单请求验证码:
public function rules(Request $request)
{
return [
'name' => 'required | string',
'details.subscriptions' => 'required | array',
'details.subscriptions.*' => 'nullable | string',
'details.size' => 'nullable | array',
'details.size.*' => 'nullable | string',
'details.notes' => 'nullable | array',
'details.notes.*' => 'nullable | string',
'details.price' => 'nullable | array',
'details.price.*' => 'nullable | double',
];
}
protected function prepareForValidation()
{
$this->merge([
'name' => ucwords($this->name),
]);
}
验证结果 - 我得到的结果:
{
"name":"Some Name",
"details":{
"subscriptions":["1"],
"size":[null,null],
"notes":["10 pcs",null],
"price":[100,null]}
}
我想得到的:
{
"name":"Some Name",
"details":{
"subscriptions":["1"],
"notes":["10 pcs"],
"price":[100]
}
}
请帮忙,我卡住了。
在控制器存储方法中试过这个:
$validated = $request->validated();
$collection = collect($validated);
$size = array_filter($validated['details']['size'], function (
$value
) {
return !is_null($value);
});
这个returns一个空数组。但是我不知道下一步该做什么。
您可以在有 null
值时进行过滤,如果数组现在为空则再次过滤。有多种方法可以实现,这只是一种可能的解决方案。
<?php
$validated = json_decode('{
"name":"Some Name",
"details":{
"subscriptions":["1"],
"size":[null,null],
"notes":["10 pcs",null],
"price":[100,null]}
}', true);
//filter null values
foreach($validated['details'] as $key => $detail) {
$validated['details'][$key] = array_filter($validated['details'][$key], function ($value) {
return !is_null($value);
});
}
//filter empty arrays
$validated['details'] = array_filter($validated['details'], function ($value) {
return !empty($value);
});
print_r($validated);
你可以做一个递归过滤器。
public function index(YourRequest $request)
{
$validated = $request->validated();
$validated = $this->filterRecursive($validated);
dd($validated);
}
public function filterRecursive($values)
{
foreach ($values as &$value) {
if (is_array($value)) {
$value = $this->filterRecursive($value);
}
}
return array_filter($values);
}