laravel 验证阵列中不在相关表中的ID

n53p2ov0  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(90)

我有一个端点,它接受以下格式的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...

xdnvmnnf

xdnvmnnf1#

你可以通过几种不同的方式来做到这一点:

  • 关闭:
'data.*.id' => [
    'required',
    'integer',
    'distinct',
    Rule::exists('posts', 'posts.id')->where(function (Builder $query) {
        return $query->where('posts.is_default', false);
    }),
    function (string $attribute, mixed $value, Closure $fail) {
        if (DB::table('comments')->where('post_id', $value)->count() !== 0) {
            $fail('Post has comments!');
        }
    },
],
  • 自定义规则:
<?php

namespace App\Rules;
 
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class PostWithoutCommentsRule implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (DB::table('comments')->where('post_id', $value)->count() !== 0) {
            $fail('Post has comments!');
        }
    }
}

'data.*.id' => [
    'required',
    'integer',
    'distinct',
    Rule::exists('posts', 'posts.id')->where(function (Builder $query) {
        return $query->where('posts.is_default', false);
    }),
    new PostWithoutCommentsRule,
],
  • 验证程序之后

这真的取决于你使用它的频率。如果它很简单,我就使用闭包。如果它很复杂,我会在之后使用验证器。如果它被使用了不止一次,无论复杂程度如何,我都会使用自定义规则。

相关问题