'require|number', 'name|套餐名称' => 'require|max:32|checkExist', 'price|套餐价格' => 'require|checkPrice', 'type_id|套餐类型' => 'require|number|in:0,1', 'op_count|套餐操作次数' => 'require|number|min:1', 'status|套餐状态' => 'require|number|in:0,1', 'sort|排序' => 'number', ]; protected $message = [ 'id.require' => 'ID不可为空', 'id.number' => 'ID必须为数字', ]; protected $scene = [ 'id' => ['id'], 'add' => ['name', 'price', 'type_id', 'op_count', 'status'], 'edit' => ['id', 'name', 'price', 'type_id', 'op_count', 'sort', 'status'], 'del' => ['id'], ]; public static function checkExist($value, $rule, $data=[]) { $where = []; if (isset($data['id'])) { $where[] = ['id', '<>', $data['id']]; } $where[] = ['name', '=', $value]; $res = ShopGoodsRenew::where($where)->find(); if ($res) { return '该名称已存在'; } return true; } public static function checkPrice($value, $rule, $data=[]) { $isAmount = preg_match('/^\d+(\.\d{1,2})?$/', $value); if(!$isAmount){ return '价格填写格式-不是金额'; } return true; } }