Validator.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace UCore;
  3. use Inhere\Validate\Validator\AbstractValidator;
  4. use function _\property;
  5. /**
  6. * 验证类 基类
  7. *
  8. */
  9. abstract class Validator extends AbstractValidator
  10. {
  11. public function __construct(public ValidationCore $validation, public array $args = [], public $message = '')
  12. {
  13. if (method_exists($this, 'prepare')) {
  14. $this->prepare();
  15. }
  16. }
  17. /**
  18. * 给验证器设置数据
  19. *
  20. * @param $name
  21. * @param $value
  22. * @return void
  23. */
  24. public function validationSet($name, $value)
  25. {
  26. if (property_exists($this->validation, $name)) {
  27. $this->validation->$name = $value;
  28. }
  29. }
  30. /**
  31. * 增加一个错误消息,并返回False
  32. * @param string $msg 错误的消息
  33. * @param string $field 发生错误的字段
  34. * @return false
  35. */
  36. protected function addError(string $msg, string $field = '')
  37. {
  38. $this->validation->addError($field, $msg);
  39. return false;
  40. }
  41. /**
  42. * 增加一个模板的错误消息,并返回False
  43. * @param $value
  44. * @param string $message 消息模版
  45. * @param string $msg
  46. * @return mixed
  47. *
  48. */
  49. public function addErrorTpl($p, $msgTpl, $field = '')
  50. {
  51. // dd(strtr($msgTpl, $p),$msgTpl,$p);
  52. $this->validation->addError($field, strtr($msgTpl, $p));
  53. return false;
  54. }
  55. }