theriddleofenigma/laravel-model-validation

v1.5.0 2024-04-05 14:56 UTC

This package is auto-updated.

Last update: 2024-09-05 15:47:26 UTC


README

♥ 使用 <love/> 制作,我热爱 <code/>

Laravel模型验证

FOSSA Status

模型验证 - 验证模型数据。 *仅适用于Laravel应用。

为Eloquent模型提供简单验证器选项。同时也提供在验证前后执行额外代码的灵活性。

Composer安装

composer require theriddleofenigma/laravel-model-validation

使用示例

这里以用户模型为例。您可以在任何模型中使用此功能。

User.php 模型

use Enigma\ValidatorTrait;

class User extends Model
{
    use ValidatorTrait;

    /**
     * Boot method.
     */
    public static function boot()
    {
        parent::boot();

        // Add this method for validating the current model on model saving event
        static::validateOnSaving();
    }

    public $validationRules = [
        'name' => 'required|max:10',
        'email' => 'required|email',
    ];

    public $validationMessages = [
        'name.required' => 'Name field is required.',
        'email.email' => 'The given email is in invalid format.',
    ];

    public $validationAttributes = [
        'name' => 'User Name'
    ];

    /**
     * Code to be executed before the validation goes here.
     */
    public function beforeValidation()
    {
        // Some code goes here..
    }

    /**
     * Code to be executed after the validation goes here.
     */
    public function afterValidation()
    {
        // Some code goes here..
    }
}

控制要验证的数据

您可以通过添加validationData方法来控制要验证的数据。

/**
 * Validation data to be validated.
 *
 * @return array
 */
public function validationData(array $data)
{
    // Here $data is the value of $this->getAttributes(), feel free to use your own code to produce the data. Ex: $this->toArray(), $this->getOriginal(), etc.,
    $data["name"] = strtolower($data["name"]);

    // Note: This wouldn't affect your actual model data which is going to persist in DB.
    
    return $data;
}

其他选项

您可以将验证规则、属性和消息作为属性或方法提及。

/**
 * Validation rules to validate.
 *
 * @return array
 */
public function validationRules()
{
    // You can process your code here and return the rules as however you want.
    return [
        'name' => 'required|max:10',
        'email' => 'required|email',
    ];
}

/**
 * Custom messages to replace the validation messages.
 *
 * @return array
 */
public function validationMessages()
{
    // You can process your code here and return the messages as however you want.
    return [
        'name.required' => 'Name field is required.',
        'email.email' => 'The given email is in invalid format.',
    ];
}

/**
 * Custom attribute names to replace the validation attribute name.
 *
 * @return array
 */
public function validationAttributes()
{
    return [
        'name' => 'User Name'
    ];
}

您可以为创建操作本身或任何模型事件进行验证,只需添加 $model->validate()

/**
 * Boot method.
 */
public static function boot()
{
    parent::boot();

    // You can mention like this for validating the model on custom events as your wish
    self::creating(function($model){
        $model->validate();
    });

    // Or you can make use of the alias `self::validateOnCreating()`.
}

参考ValidationTrait中的可用方法。

许可证

Laravel模型验证是开源软件,许可协议为MIT许可证

FOSSA Status