moves/eloquent-validatable

此包已弃用且不再维护。未建议替代包。

3.0.0 2023-04-10 16:13 UTC

This package is auto-updated.

Last update: 2023-12-18 17:53:30 UTC


README

简介

Eloquent Validatable 是一组用于 Eloquent 模型的函数,它封装了 Laravel 的验证器。在保存模型类时自动验证,从而减少控制器和其他应用程序代码中的冗余。

安装

要将此库添加到您的项目中,请运行

composer require moves/eloquent-validatable

使用方法

基本配置

使用 Validatable 接口和特质创建您的 Eloquent 模型,然后添加配置设置。

use Moves\Eloquent\Validatable\Interfaces\IValidatable;
use Moves\Eloquent\Validatable\Validatable;

class YourModel implements IValidatable {
    use Validatable;
    
    protected $validateOnSave = true;
    protected $validationRules = [...];
    protected $validationMessages = [...];
    protected $validationCustomAttributes = [...];
}

要在 Eloquent 保存时自动验证模型属性,将 $validateOnSave 设置为 true。默认情况下,此行为是禁用的。

高级配置

对于动态配置,实现配置访问器函数而不是设置属性。这允许您在底层验证器使用的值周围应用自定义逻辑。

class YourModel implements IValidatable {
    use Validatable;
    
    public getValidateOnSave(): bool {
        return true;
    }
    
    public getValidationData(): array {
        return [...];
    }
    
    public getValidationRules(): array {
        return [...];
    }
    
    public getValidationMessages(): array {
        return [...];
    }
    
    public getValidationCustomAttributes(): array {
        return [...];
    }
}