validate() public static method

实例化验证类 格式:[模块名/]验证器名
public static validate ( string $name = '', string $layer = 'validate', boolean $appendSuffix = false, string $common = 'common' ) : Object | false
$name string 资源地址
$layer string 验证层名称
$appendSuffix boolean 是否添加类名后缀
$common string 公共模块名
return Object | false
Exemplo n.º 1
0
 /**
  * 登陆
  * @param  string $callback 登陆成功后的回调地址
  */
 public function index($callback = '')
 {
     if (IS_POST) {
         $validate = Loader::validate('Login');
         $data = $this->request->post();
         if (config('verify_code')) {
             $validateResult = $validate->check($data);
         } else {
             $validateResult = $validate->scene('not_verify')->check($data);
         }
         if (!$validateResult) {
             return $this->error($validate->getError(), '');
         }
         $user = Db::name('Member')->where('account', $data['account'])->find();
         if (!$user) {
             return $this->error('用户不存在', '');
         } elseif ($user['status'] != 1) {
             return $this->error('用户被禁用', '');
         } elseif ($user['password'] != umd5($data['password'])) {
             logs('登陆失败:密码错误', '', $user['id']);
             return $this->error('密码错误', '');
         } else {
             self::autoLogin($user);
             return $this->success('登陆成功', $callback ? $callback : url('system/index/index'));
         }
     } else {
         if (isLogin()) {
             $this->redirect(url('system/index/index'));
         }
         return view();
     }
 }
Exemplo n.º 2
0
Arquivo: Auth.php Projeto: cjango/cwms
 /**
  * [add description]
  */
 public function add()
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('Auth');
         if (!$validate->check($data)) {
             return $this->error($validate->getError());
         }
         $data = ['title' => $data['title'], 'create_time' => NOW_TIME, 'update_time' => 0, 'status' => $data['status'], 'remark' => $data['remark'], 'rules' => ''];
         if (Db::name('Auth')->insert($data)) {
             return $this->success();
         } else {
             return $this->error();
         }
     } else {
         return $this->fetch('edit');
     }
 }
Exemplo n.º 3
0
 /**
  * 修改密码
  */
 public function password()
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('Member');
         if (!$validate->scene('changepass')->check($data)) {
             return $this->error($validate->getError());
         }
         $passData = ['password' => umd5($data['newpass']), 'update_time' => NOW_TIME];
         if (Db::name('Member')->where('id', UID)->update($passData)) {
             return $this->success('密码修改成功');
         } else {
             return $this->error();
         }
     } else {
         return $this->fetch();
     }
 }
Exemplo n.º 4
0
 /**
  * [edit description]
  * @param  integer $id [description]
  */
 public function edit($id)
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('Category');
         if (!$validate->check($data)) {
             return $this->error($validate->getError());
         }
         if (Loader::model('Category')->update($data)) {
             return $this->success();
         } else {
             return $this->error();
         }
     } else {
         $this->assign('info', Db::name('Category')->find($id));
         $this->assign('upcate_list', Loader::model('Category')->treeSelect('', $id));
         return $this->fetch('edit');
     }
 }
Exemplo n.º 5
0
Arquivo: Menu.php Projeto: cjango/cwms
 /**
  * 编辑
  * @param  [type] $id 主键
  */
 public function edit($id)
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('Menu');
         if (!$validate->check($data)) {
             return $this->error($validate->getError());
         }
         if (Loader::model('Menu')->update($data)) {
             session('system_menu_list', null);
             return $this->success();
         } else {
             return $this->error();
         }
     } else {
         $this->assign('up_menus', self::_treeShow($id));
         $this->assign('info', Db::name('Menu')->where('id', $id)->find());
         return $this->fetch();
     }
 }
Exemplo n.º 6
0
 /**
  * 自动验证数据
  * @access protected
  * @param array $data 验证数据
  * @return bool
  */
 protected function validateData($data)
 {
     if (!empty($this->validate)) {
         $info = $this->validate;
         if (is_array($info)) {
             $validate = Loader::validate();
             $validate->rule($info['rule']);
             $validate->message($info['msg']);
         } else {
             $name = is_string($info) ? $info : $this->name;
             if (strpos($name, '.')) {
                 list($name, $scene) = explode('.', $name);
             }
             $validate = Loader::validate($name);
             if (!empty($scene)) {
                 $validate->scene($scene);
             }
         }
         if (!$validate->check($data)) {
             $this->error = $validate->getError();
             if ($this->failException) {
                 throw new ValidateException($this->error);
             } else {
                 return false;
             }
         }
         $this->validate = null;
     }
     return true;
 }
Exemplo n.º 7
0
 /**
  * 实例化验证器
  * @param string    $name 验证器名称
  * @param string    $layer 业务层名称
  * @param bool      $appendSuffix 是否添加类名后缀
  * @return \think\Validate
  */
 function validate($name = '', $layer = 'validate', $appendSuffix = false)
 {
     return Loader::validate($name, $layer, $appendSuffix);
 }
Exemplo n.º 8
0
 /**
  * 验证数据
  * @access protected
  * @param array        $data     数据
  * @param string|array $validate 验证器名或者验证规则数组
  * @param array        $message  提示信息
  * @param bool         $batch    是否批量验证
  * @param mixed        $callback 回调方法(闭包)
  * @return array|string|true
  * @throws ValidateException
  */
 protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
 {
     if (is_array($validate)) {
         $v = Loader::validate();
         $v->rule($validate);
     } else {
         if (strpos($validate, '.')) {
             // 支持场景
             list($validate, $scene) = explode('.', $validate);
         }
         $v = Loader::validate($validate);
         if (!empty($scene)) {
             $v->scene($scene);
         }
     }
     // 是否批量验证
     if ($batch || $this->batchValidate) {
         $v->batch(true);
     }
     if (is_array($message)) {
         $v->message($message);
     }
     if ($callback && is_callable($callback)) {
         call_user_func_array($callback, [$v, &$data]);
     }
     if (!$v->check($data)) {
         if ($this->failException) {
             throw new ValidateException($v->getError());
         } else {
             return $v->getError();
         }
     } else {
         return true;
     }
 }
Exemplo n.º 9
0
 /**
  * 编辑配置
  * @param  integer $id 配置主键
  */
 public function edit($id)
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('Config');
         if (!$validate->check($data)) {
             return $this->error($validate->getError());
         }
         if (Loader::model('Config')->update($data)) {
             Cache::clear();
             return $this->success();
         } else {
             return $this->error();
         }
     } else {
         $this->assign('info', Db::name('Config')->find($id));
         return $this->fetch();
     }
 }
Exemplo n.º 10
0
 /**
  * 自动验证当前数据对象值
  * @access public
  * @return bool
  */
 public function validateData()
 {
     if (!empty($this->validate)) {
         $info = $this->validate;
         if (is_array($info)) {
             $validate = Loader::validate(Config::get('default_validate'));
             $validate->rule($info['rule']);
             $validate->message($info['msg']);
         } else {
             $name = is_string($info) ? $info : $this->name;
             if (strpos($name, '.')) {
                 list($name, $scene) = explode('.', $name);
             }
             $validate = Loader::validate($name);
             if (!empty($scene)) {
                 $validate->scene($scene);
             }
         }
         if (!$validate->check($this->data)) {
             $this->error = $validate->getError();
             return false;
         }
         $this->validate = null;
     }
     return true;
 }
Exemplo n.º 11
0
 /**
  * 编辑关键字
  * @return [type]
  */
 public function keyword_edit($id)
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('WechatKeyword');
         if (!$validate->check($data)) {
             return $this->error($validate->getError());
         }
         if (Loader::model('WechatKeyword')->update($data)) {
             return $this->success('', url('system/weixin/keyword'));
         } else {
             return $this->error();
         }
     } else {
         $info = Db::name('WechatKeyword')->find($id);
         $this->assign('contentType', self::keyword_type($info['type'], $info['content']));
         $this->assign('info', $info);
         return $this->fetch();
     }
 }
Exemplo n.º 12
0
 /**
  * 编辑
  * @param  [type] $id [description]
  */
 public function edit($id)
 {
     if (IS_POST) {
         $data = $this->request->post();
         $validate = Loader::validate('Member');
         if (!$validate->scene('edit')->check($data)) {
             return $this->error($validate->getError());
         }
         if (Loader::model('Member')->update($data)) {
             logs('用户编辑成功', $data);
             return $this->success();
         } else {
             return $this->error();
         }
     } else {
         $level = Db::name('member')->find($id);
         $map = ['id' => ['neq', 1], 'status' => 1, 'level' => $level['level'] - 1];
         $list = Db::name('member')->where($map)->field('id,nickname')->select();
         $this->assign('list', $list);
         $info = Db::name('Member')->find($id);
         $this->assign('info', $info);
         return $this->fetch();
     }
 }
Exemplo n.º 13
0
 /**
  * 验证数据
  * @access protected
  * @param array $data 数据
  * @param string|array $validate 验证器名或者验证规则数组
  * @param array $message 提示信息
  * @param mixed $callback 回调方法(闭包)
  * @return true|string|array
  */
 public function validate($data, $validate, $message = [], $callback = null)
 {
     if (is_array($validate)) {
         $v = Loader::validate(Config::get('default_validate'));
         $v->rule($validate);
     } else {
         if (strpos($validate, '.')) {
             // 支持场景
             list($validate, $scene) = explode('.', $validate);
         }
         $v = Loader::validate($validate);
         if (!empty($scene)) {
             $v->scene($scene);
         }
     }
     if (is_array($message)) {
         $v->message($message);
     }
     if (is_callable($callback)) {
         call_user_func_array($callback, [$v, &$data]);
     }
     if (!$v->check($data)) {
         return $v->getError();
     } else {
         return true;
     }
 }