AdminUserRequest::messages()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Http\Requests\Admin;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use App\Model\Admin\AdminUser;
7
use Illuminate\Validation\Rule;
8
9
class AdminUserRequest extends FormRequest
10
{
11
    /**
12
     * Determine if the user is authorized to make this request.
13
     *
14
     * @return bool
15
     */
16
    public function authorize()
17
    {
18
        return true;
19
    }
20
21
    /**
22
     * Get the validation rules that apply to the request.
23
     *
24
     * @return array
25
     */
26
    public function rules()
27
    {
28
        $status_in = [
29
            AdminUser::STATUS_DISABLE,
30
            AdminUser::STATUS_ENABLE,
31
        ];
32
33
        $passwordRule = '';
34
        if ($this->method() == 'POST' ||
35
            ($this->method() == 'PUT' && request()->post('password') !== '')) {
36
            $passwordRule = [
37
                'required',
38
                'regex:/^(?![0-9]+$)(?![a-zA-Z]+$)[\w\x21-\x7e]{6,18}$/'
39
            ];
40
        }
41
        return [
42
            'name' => 'required|max:50',
43
            'password' => $passwordRule,
44
            'status' => [
45
                Rule::in($status_in),
46
            ],
47
        ];
48
    }
49
50
    /**
51
     * Get custom messages for validator errors.
52
     *
53
     * @return array
54
     */
55
    public function messages()
56
    {
57
        return [
58
            'name.required' => '用户名不能为空',
59
            'password.required' => '密码不能为空',
60
            'regex' => '密码不符合规则'
61
        ];
62
    }
63
}
64