| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <?php
- namespace UCore\Validator;
- use UCore\ValidationCore;
- use UCore\Validator;
- /**
- * 数据验证器
- *
- * 使用指定的Validation类验证整个数据
- */
- class DataValidation extends Validator
- {
- /**
- * 验证数据
- *
- * args[0] 验证器类名
- *
- * @param mixed $values 要验证的值
- * @param array $data 所有验证数据
- * @return bool 验证是否通过
- */
- public function validate(mixed $values, array $data): bool
- {
- $validationClass = $this->args[0];
- /**
- * @var ValidationCore $validation
- */
- $validation = new $validationClass($data);
- $validation->validate();
- if ($validation->isFail()) {
- $errorMessage = $validation->firstError();
- $this->addError("数据验证失败: {$errorMessage}");
- return false;
- }
- return true;
- }
- }
|