Files
dafuweng/addons/webman/form/Validator.php
2026-03-02 13:44:38 +08:00

54 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace addons\webman\form;
use ExAdmin\ui\contract\ValidatorAbstract;
use ExAdmin\ui\response\Response;
class Validator extends ValidatorAbstract
{
/**
* 验证
* @param array $data 表单数据
* @param bool $edit true更新false新增
* @return mixed
*/
function check(array $data, bool $edit)
{
$ruleArr = $edit ? $this->updateRule : $this->createRule;
$rules = [];
$messages = [];
foreach ($ruleArr as $field => $row) {
$rule = [];
if($row instanceof \Closure){
$row = call_user_func_array($row,[$data,$this->form]);
}
foreach ($row as $key => $item) {
if (is_numeric($key)) {
$rule[] = $item;
} else {
$rule[] = $key;
$index = strpos($key, ':');
if ($index !== false) {
$key = substr($key, 0, $index);
}
$messages["{$field}.{$key}"] = $item;
}
}
$rules[$field] = $rule;
}
$validator = validator($data, $rules, $messages);
if ($validator->fails()) {
return Response::success($validator->errors()->getMessages(), '', 422);
}
if($this->form->getSteps()){
if(!$this->form->isStepfinish()){
return Response::success([], '', 201);
}
}
return true;
}
}