我有一个端点,它接受以下格式的ID数组:
{
"data": [
{
"id": 1
},
{
"id": 2
},
{
"id": 4
}
]
}
...在我的验证请求文件中,我有:
<?php
namespace AppNew\Http\Requests;
use Illuminate\Validation\Rule;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
class ArchivePostsCollectionRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'data' => [
'array',
],
'data.*' => [
'required',
'array',
],
'data.*.id' => [
'required',
'integer',
'distinct',
Rule::exists('posts', 'posts.id')->where(function (Builder $query) {
return $query->where('posts.is_default', false);
}),
],
];
}
public function messages(): array
{
return [
'data.array' => 'Expected an array!',
'data.*' => 'Expected an array of objects!',
'data.*.id.required' => 'Expected an ID!',
'data.*.id.integer' => 'Expected an integer for ID!',
'data.*.id.integer' => 'Expected distinct IDs!',
'data.*.id.exists' => 'Selected post is set to default and cannot be archived!',
];
}
}
我想添加另一个规则来检查每个帖子ID是否没有评论;comments表有一个post_id列要检查。然而,我对如何从规则的Angular 来处理这一点感到困惑!
任何指针非常赞赏。
谢了K...
1条答案
按热度按时间xdnvmnnf1#
你可以通过几种不同的方式来做到这一点:
这真的取决于你使用它的频率。如果它很简单,我就使用闭包。如果它很复杂,我会在之后使用验证器。如果它被使用了不止一次,无论复杂程度如何,我都会使用自定义规则。