| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace app\common\validate;
- use Lettered\Support\Exceptions\EvidentException;
- use think\facade\Request;
- use think\Validate;
- class BaseValidate extends Validate
- {
- // 验证器检查
- public function valid()
- {
- $params = Request::param();
- if (!$this->check($params)) {
- throw new EvidentException([
- 'errmsg' => is_array($this->error) ? implode(';', $this->error) : $this->error,
- ]);
- }
- return true;
- }
- /************************* 自定义验证 ********************************/
- protected function isPositiveInteger($value, $rule='', $data='', $field='')
- {
- if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
- return true;
- }
- return $field . '必须是正整数';
- }
- protected function isNotEmpty($value, $rule='', $data='', $field='')
- {
- if (empty($value)) {
- return $field . '不允许为空';
- } else {
- return true;
- }
- }
- //没有使用TP的正则验证,集中在一处方便以后修改
- //不推荐使用正则,因为复用性太差
- //手机号的验证规则
- protected function isMobile($value)
- {
- $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
- $result = preg_match($rule, $value);
- if ($result) {
- return true;
- } else {
- return false;
- }
- }
- // 不能为空
- protected function NotEmpty($value, $rule='', $data='', $field='')
- {
- if (empty($value)) return $field."不能为空";
- return true;
- }
- }
|