| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 | <?phpnamespace app\common\validate;use think\Db;use think\Validate;class ShopGoods extends Validate{    /**     * 定义验证规则     * 格式:'字段名' =>  ['规则1','规则2'...]     *     * @var array     */    protected $rule = [        'title|商品名称'  =>  'require|length:1,20|checkUnique',        'price|商品价格'  =>  'require|gt:0',        'cate_id|商品分类'  =>  'require|gt:0',        'img|商品图片'  =>  'require',    ];    /**     * 定义错误信息     * 格式:'字段名.规则名' =>  '错误信息'     *     * @var array     */    protected $message = [        'cate_id.require'=>'商品分类不能为空',        'cate_id.gt'=>'商品分类不能为空'    ];    protected function checkUnique($value, $rule, $data=[]){        if(isset($data['id']) && $data['id'] > 0){            $ret = Db::name('shop_goods')                ->where('del',0)                ->where('title',$data['title'])                ->where('org_id',$data['org_id'])                ->where('id','<>',$data['id'])                ->find();        }else{            $ret = Db::name('shop_goods')                ->where('del',0)                ->where('title',$data['title'])                ->where('org_id',$data['org_id'])                ->find();        }        return $ret?'名称已被使用':true;    }}
 |