Files

87 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace plugin\produce\app\validate;
use core\base\BaseValidate;
use Illuminate\Validation\Rule;
use plugin\produce\app\model\StepRelation;
class StepRelationValidate extends BaseValidate
{
public function rules(): array
{
$id = request()->route?->param('id');
$fromStepId = (string) (request()->input('from_step_id') ?? '');
$toStepId = (string) (request()->input('to_step_id') ?? '');
$relationType = (string) (request()->input('relation_type') ?? 'normal');
return [
'from_step_id' => ['required', 'integer'],
'to_step_id' => ['required', 'integer', 'different:from_step_id'],
'relation_type' => [
'required',
'max:20',
Rule::in(['normal', 'exception']),
Rule::unique(StepRelation::class, 'relation_type')
->where(fn ($query) => $query
->where('from_step_id', $fromStepId)
->where('to_step_id', $toStepId)
)
->ignore($id, 'id')
],
'flow_condition' => [
'nullable',
function (string $attribute, mixed $value, \Closure $fail) {
if ($value === null || $value === '') {
return;
}
if (is_array($value)) {
return;
}
if (!is_string($value)) {
$fail('流转条件必须为有效的JSON字符串');
return;
}
json_decode($value, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$fail('流转条件必须为有效的JSON字符串');
}
}
],
'priority' => ['nullable', 'integer', 'min:0'],
'description' => ['nullable', 'max:65535'],
'sort' => ['nullable', 'integer', 'min:0'],
'status' => ['nullable', 'integer', 'in:0,1'],
];
}
protected array $message = [
'from_step_id.required' => '源步骤不能为空',
'from_step_id.integer' => '源步骤格式不正确',
'to_step_id.required' => '目标步骤不能为空',
'to_step_id.integer' => '目标步骤格式不正确',
'to_step_id.different' => '目标步骤不能与源步骤相同',
'relation_type.required' => '关系类型不能为空',
'relation_type.max' => '关系类型长度不能超过20个字符',
'relation_type.in' => '关系类型值不正确',
'relation_type.unique' => '该步骤关系已存在',
'priority.integer' => '优先级必须为整数',
'priority.min' => '优先级不能小于0',
'description.max' => '描述内容过长',
'sort.integer' => '排序必须为整数',
'sort.min' => '排序不能小于0',
'status.integer' => '状态格式不正确',
'status.in' => '状态值不正确',
];
protected array $scene = [
'store' => ['from_step_id', 'to_step_id', 'relation_type', 'flow_condition', 'priority', 'description', 'sort', 'status'],
'update' => ['from_step_id', 'to_step_id', 'relation_type', 'flow_condition', 'priority', 'description', 'sort', 'status'],
];
}