<?php
namespace app\common\validate;

use think\Db;
use think\Validate;

class Roles extends Validate{

    protected $rule = [
        'name|名称'  =>  'require|length:1,50|checkTitle',
        'parent_id|类型' => 'require|gt:0',
    ];

    protected $message = [
        'name.length' => '名称必须50字以内',
        'parent_id.require' => '未选择类型',
        'parent_id.gt' => '未选择类型',
    ];

    protected $scene = [

    ];

    protected function checkTitle($value,$rule,$data=[])
    {
        $info = Db::name('roles')->where('name',$value)->where('del',0)->where('org_id',$data['org_id'])->find();
        if($data['id'] <= 0 && $info){
            return '名称已被使用';
        }
        if($info && $data['id'] > 0 && $info['id'] != $data['id']){
            return '名称已被使用';
        }
        return true;
    }

}