UserRequest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 3 1
A rules() 0 20 2
A messages() 0 6 1
1
<?php
2
3
namespace App\Http\Requests\Admin;
4
5
use App\Foundation\Regexp;
6
use Illuminate\Foundation\Http\FormRequest;
7
use App\Model\Admin\User;
8
use Illuminate\Validation\Rule;
9
10
class UserRequest extends FormRequest
11
{
12
    /**
13
     * Determine if the user is authorized to make this request.
14
     *
15
     * @return bool
16
     */
17
    public function authorize()
18
    {
19
        return true;
20
    }
21
22
    /**
23
     * Get the validation rules that apply to the request.
24
     *
25
     * @return array
26
     */
27
    public function rules()
28
    {
29
        $status_in = [
30
            User::STATUS_DISABLE,
31
            User::STATUS_ENABLE,
32
        ];
33
        $rule = [
34
            'phone' => [
35
                'required',
36
                'regex:/' . Regexp::PHONE . '/',
37
            ],
38
            'status' => [
39
                Rule::in($status_in),
40
            ],
41
        ];
42
        if ($this->method() == 'POST') {
43
            $rule['password'] = ['required', 'regex:/' . Regexp::PASSWORD . '/'];
44
        }
45
46
        return $rule;
47
    }
48
49
    /**
50
     * Get custom messages for validator errors.
51
     *
52
     * @return array
53
     */
54
    public function messages()
55
    {
56
        return [
57
            'phone.required' => '手机号不能为空',
58
            'phone.regex' => '手机号格式有误',
59
            'password.regex' => '密码格式有误',
60
        ];
61
    }
62
}
63