ForeachValidator.php 942 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace UCore\Validator;
  3. use UCore\ValidationCore;
  4. use UCore\Validator;
  5. /**
  6. *
  7. * 遍历数组,使用Validator
  8. *
  9. */
  10. class ForeachValidator extends Validator
  11. {
  12. public function validate(mixed $values, array $data): bool
  13. {
  14. $vaClass = $this->args[0];
  15. // "数值{value}没有通过验证"
  16. $message =$this->message;
  17. foreach ($values as $k => $value) {
  18. $data = [
  19. $value
  20. ];
  21. /**
  22. * @var Validator $va
  23. */
  24. $va = new $vaClass($this->validation);
  25. $res = $va->validate($value, $data);
  26. if (!$res) {
  27. $p = [
  28. '{index}'=>$k,
  29. '{value}'=>$value,
  30. '{message}'=>''
  31. ];
  32. $this->addErrorTpl($p, $message);
  33. return false;
  34. }
  35. }
  36. return true;
  37. }
  38. }