ProductRequest.php
3.8 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace App\Http\Requests\Bside\Product;
use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Models\Product\Product;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
/**
* Class ProductRequest
* @package App\Http\Requests\Bside\product
* @author zbj
* @date 2023/4/12
*/
class ProductRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|max:200',
'route' => 'required|max:200',
'gallery' => ['required', 'array', function ($attribute, $value, $fail) {
foreach ($value as $v) {
if (empty($v['url'])) {
$fail('图片链接不能为空');
}
if (!Str::contains($v['url'], env('UPLOAD_LOCAL_URL') ?: env('APP_URL'))) {
$fail('图片链接不正确');
}
}
}],
'attrs' => ['array', function ($attribute, $value, $fail) {
foreach ($value as $v) {
if (empty($v['key'])) {
$fail('产品属性名不能为空');
}
if (empty($v['value'])) {
$fail('产品属性值不能为空');
}
}
}],
'category_id' => 'required',
'intro' => 'required|max:500',
'content' => 'required',
'seo_mate' => ['array', function ($attribute, $value, $fail) {
if(empty($value['title'])){
$fail('SEO标题不能为空');
}
if(empty($value['description'])){
$fail('SEO描述不能为空');
}
if(empty($value['keyword'])){
$fail('SEO关键词不能为空');
}
}],
'related_product_id' => [function ($attribute, $value, $fail) {
$value = array_filter(Arr::splitFilterToArray($value), 'intval');
if(count($value) > 16){
$fail('关联产品不能超过16个');
}
}],
'status' => ['required', Rule::in(array_keys(Product::statusMap()))],
];
}
public function messages()
{
return [
'title.required' => '请输入产品标题',
'title.max' => '产品标题不能超过20个字符',
'route.required' => '请输入产品链接',
'route.max' => '产品链接不能超过200个字符',
'gallery.required' => '请上传产品图片',
'gallery.array' => '产品图片格式异常',
'attrs.required' => '请添加产品参数',
'attrs.array' => '产品参数格式异常',
'category_id.required' => '请选择分类',
'keywords.required' => '请添加关键词标签',
'intro.required' => '请输入短描述',
'intro.max' => '短描述不能超过500个字符',
'content.required' => '请输入产品描述',
'describe.required' => '请添加描述切换栏',
'describe.array' => '描述切换栏格式异常',
'seo_mate.required' => '请输入SEO',
'seo_mate.array' => 'SEO格式异常',
'related_product_id.required' => '请选择相关产品',
'status.required' => '请选择产品状态',
'status.in' => '产品状态值异常',
];
}
}