BaseValidate.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\common\validate;
  3. use Lettered\Support\Exceptions\EvidentException;
  4. use think\facade\Request;
  5. use think\Validate;
  6. class BaseValidate extends Validate
  7. {
  8. // 验证器检查
  9. public function valid()
  10. {
  11. $params = Request::param();
  12. if (!$this->check($params)) {
  13. throw new EvidentException([
  14. 'errmsg' => is_array($this->error) ? implode(';', $this->error) : $this->error,
  15. ]);
  16. }
  17. return true;
  18. }
  19. /************************* 自定义验证 ********************************/
  20. protected function isPositiveInteger($value, $rule='', $data='', $field='')
  21. {
  22. if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
  23. return true;
  24. }
  25. return $field . '必须是正整数';
  26. }
  27. protected function isNotEmpty($value, $rule='', $data='', $field='')
  28. {
  29. if (empty($value)) {
  30. return $field . '不允许为空';
  31. } else {
  32. return true;
  33. }
  34. }
  35. //没有使用TP的正则验证,集中在一处方便以后修改
  36. //不推荐使用正则,因为复用性太差
  37. //手机号的验证规则
  38. protected function isMobile($value)
  39. {
  40. $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
  41. $result = preg_match($rule, $value);
  42. if ($result) {
  43. return true;
  44. } else {
  45. return false;
  46. }
  47. }
  48. // 不能为空
  49. protected function NotEmpty($value, $rule='', $data='', $field='')
  50. {
  51. if (empty($value)) return $field."不能为空";
  52. return true;
  53. }
  54. }