syehan/laravel-model-validation
Composer 安装命令:
composer require syehan/laravel-model-validation
包简介
Model validation for Laravel application.
README 文档
README
♥ Made with <love/> And I love <code/>
Laravel Model Validation
Model validation - Validates the model data. *Only for laravel applications.
An easy validator option for your eloquent models. Also have flexibility for additional codes that might be executed on before and after validation.
Composer install
composer require theriddleofenigma/laravel-model-validation
Usage Examples
Here user model is mentioned as an example. You could use this in any model you want.
User.php model
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..
}
}
Other options
You could mention the validation rules, attributes and messages as a property as well as method.
/**
* 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'
];
}
You could mention the validation only for creating itself or on any model event just add $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()`.
}
Refer the available methods in the ValidationTrait.
License
Laravel Model Validation is open-sourced software licensed under the MIT license.
syehan/laravel-model-validation 适用场景与选型建议
syehan/laravel-model-validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 552 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 syehan/laravel-model-validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 syehan/laravel-model-validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 552
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2022-12-28