korridor/laravel-model-validation-rules

一个使用 Eloquent 验证模型是否存在 laravel 验证规则

3.1.0 2024-02-28 15:07 UTC

This package is auto-updated.

Last update: 2024-09-06 12:06:25 UTC


README

Latest Version on Packagist License Supported PHP versions GitHub Workflow Lint GitHub Workflow Tests Codecov

此包是 Laravel 内置验证规则 existsunique 的替代品。它使用 Eloquent 模型而不是直接查询数据库。

优点

  • 规则可以轻松地通过 Eloquent 构建器进行扩展。(作用域等。)
  • 软删除开箱即用。
  • 模型中实现的逻辑在验证中也有效。(多租户系统等。)

注意

请查看 solidtime - 现代开源时间追踪器solidtime.io

安装

您可以使用以下命令通过 composer 安装此包:

composer require korridor/laravel-model-validation-rules

如果您想使用此包与较旧的 Laravel/PHP 版本,请安装 2.1.* 版本。

composer require korridor/laravel-model-validation-rules "^2.1"

要求

此包已测试以下 Laravel 和 PHP 版本

  • 10.* (PHP 8.1, 8.2, 8.3)
  • 11.* (PHP 8.2, 8.3)

使用示例

PostStoreRequest

use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
// ...
public function rules(): array
{
    $postId = $this->post->id;
    
    return [
        'username' => [new UniqueEloquent(User::class, 'username')],
        'title' => ['string'],
        'content' => ['string'],
        'comments.*.id' => [
            'nullable',
            new ExistsEloquent(Comment::class, null, function (Builder $builder) use ($postId) {
                return $builder->where('post_id', $postId);
            }),
        ],
        'comments.*.content' => ['string']
    ];
}

PostUpdateRequest

use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
// ...
public function rules(): array
{
    $postId = $this->post->id;
    
    return [
        'id' => [new ExistsEloquent(Post::class)],
        'username' => [(new UniqueEloquent(User::class, 'username'))->ignore($postId)],
        'title' => ['string'],
        'content' => ['string'],
        'comments.*.id' => [
            'nullable',
            new ExistsEloquent(Comment::class, null, function (Builder $builder) use ($postId) {
                return $builder->where('post_id', $postId);
            }),
        ],
        'comments.*.content' => ['string']
    ];
}

自定义验证消息

如果您想更改特定情况的验证消息,可以使用 withMessage(...) 函数添加自定义验证消息。使用 withCustomTranslation(...) 您可以为验证消息设置自定义翻译键。如以下示例中详细描述的(自定义默认验证消息),可以使用 :attribute:model:value 在翻译中。

use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
// ...
public function rules(): array
{
    $postId = $this->post->id;
    
    return [
        'id' => [(new ExistsEloquent(Post::class))->withMessage('The ID already exists.')],
        'username' => [
            (new UniqueEloquent(User::class, 'username'))
                ->ignore($postId)
                ->withCustomTranslation('validation.custom.username.unique_eloquent')
        ],
        'title' => ['string'],
        'content' => ['string'],
        'comments.*.id' => [
            'nullable',
            new ExistsEloquent(Comment::class, null, function (Builder $builder) use ($postId) {
                return $builder->where('post_id', $postId);
            }),
        ],
        'comments.*.content' => ['string']
    ];
}

自定义默认验证消息

如果您想自定义默认验证错误的翻译,可以将包的翻译发布到 resources/lang/vendor/modelValidationRules 文件夹。

php artisan vendor:publish --provider="Korridor\LaravelModelValidationRules\ModelValidationServiceProvider"

您可以在验证消息中使用以下属性

  • attribute
  • model
  • value
return [
    'exists_model' => 'A :model with the :attribute ":value" does not exist.',
    'unique_model' => 'A :model with the :attribute ":value" already exists.',
];

示例输出将如下

  • 用户 "2" 不存在。
  • 用户 "2" 已存在。

贡献

我欢迎建议和贡献。只需创建一个问题或拉取请求。

本地 Docker 环境

docker 文件夹中包含用于开发的本地 Docker 环境。Docker 工作空间已安装 composer 和 xdebug。

docker-compose run workspace bash

测试

composer test 命令使用 phpunit 运行所有测试。 composer test-coverage 命令使用 phpunit 运行所有测试并在 coverage 文件夹中创建覆盖率报告。

代码格式化/检查

composer fix 命令使用 php-cs-fixer 格式化代码。 composer lint 命令使用 phpcs 检查代码。

致谢

仓库结构和 TestClass 的结构受到 laravel-validation-rules 项目的启发,该项目由 spatie 提供。

许可证

此包受 MIT 许可证(MIT)许可。请参阅 许可证文件 以获取更多信息。