public function boot()
{
/**
* Inspired by @see \Illuminate\Foundation\Providers\FormRequestServiceProvider::boot()
*
* Note: saving event is always triggered before creating and updating events
*/
$this->app['events']->listen('eloquent.saving: *', function (string $event_name, array $data): void {
/** @var \App\Extensions\Illuminate\Database\Eloquent\Model $object */
$object = $data[0];
$object->validate();
});
}
// \App\Extensions\Illuminate\Database\Eloquent\Model
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Validation\ValidationException;
abstract class Model extends EloquentModel
{
public function validate():void
{
// 1. validate type rules (type-check)
$validator = $this->getTypeValidator();
if ($validator->fails()) {
throw new ValidationException($validator);
}
// $validator = $this->getConstraintValidator();
// 2. validate constraint rules (sanity-check)
}
protected function getTypeValidator()
{
return $this->getValidationFactory()->make($this->attributes, static::COLUMN_TYPE_RULES);
}
protected function getValidationFactory()
{
return app(Factory::class);
}
protected function getConstraintValidator()
{
// return $this->getValidationFactory()->make($attributes, static::COLUMN_CONSTRAINT_RULES);
}
}
class Account extends Model
{
public const COLUMN_TYPE_RULES = [
'id' => 'integer|between:0,4294967295',
'source' => 'nullable|in:schwab,orion,yodlee',
'type' => 'required|in:bank,card,loan',
];
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有