BannerRequest.php
2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace App\Http\Container\AdminSection\Requests\System;
use Closure;
use Hikoon\LaravelApi\Support\ApiRequest;
class BannerRequest extends ApiRequest
{
public function prepareForValidation(): void
{
$this->mergeIfMissing(['content_picture' => '', 'content' => [], 'permission' => []]);
}
public function rules(): array
{
return [
'name' => 'required|string|max:200',
'scope' => 'required|numeric|in:1',
'weight' => 'required|numeric|between:0,200',
'permission' => 'sometimes|array',
'permission.*' => 'string',
'type' => 'required|numeric|in:1,2,3,4',
'cover' => 'required|url',
'content_picture' => "required_if:type,2|url",
'content' => ['required_if:type,3,4', function (string $attribute, mixed $value, Closure $fail) {
if ($this->integer('type') === 3 && !is_array($value)) {
$fail('详情类型错误');
}
if ($this->integer('type') === 4 && !is_string($value)) {
$fail('详情类型错误');
}
}]
];
}
public function messages(): array
{
return [
'name.required' => '请输入名称',
'name.max' => '名称长度不能超过:max',
'scope.required' => '请选择展示位置',
'scope.in' => '展示位置不在指定范围内',
'weight.required' => '请输入权重值',
'weight.numeric' => '权重值类型错误',
'weight.between' => '权重值不在指定范围内',
'role.array' => '浏览用户类型错误',
'type.required' => '请选择类型',
'type.in' => '类型不在指定范围内',
'cover.required' => '请上传封面',
'cover.url' => '无效的封面链接地址',
'content_picture.required_if' => '请输入详情链接',
'content_picture.url' => '无效的详情链接地址',
'content.required_if' => '详情内容不能为空',
'content.array' => '详情类型错误'
];
}
}