|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Requests; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
6
|
|
|
use App\Http\Requests\Request; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
|
|
9
|
|
|
class EditDevice 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 Auth::user()->isUser(); |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get the validation rules that apply to the request. |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public function rules() |
|
27
|
|
|
{ |
|
28
|
|
|
//TODO figure out way for unique location names for each specific site |
|
29
|
|
|
return [ |
|
30
|
|
|
'name' => 'bail|required|min:2|max:75|regex:#(^[a-zA-Z0-9])([\w ]*)(\w$)#', |
|
31
|
|
|
'open_time' => 'required|date_format:H:i', |
|
32
|
|
|
'close_time' => 'required|date_format:H:i', |
|
33
|
|
|
'update_rate' => 'required|integer|digits_between:1,7', |
|
34
|
|
|
'image_rate' => 'required|integer|digits_between:1,7', |
|
35
|
|
|
'sensor_rate' => 'required|integer|digits_between:1,7', |
|
36
|
|
|
'command' => 'sometimes|required|alpha|max:6|in:open,close,lock,unlock', |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get the validation rules that apply to the request. |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Illuminate\Contracts\Validation\Validator |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function getValidatorInstance() |
|
46
|
|
|
{ |
|
47
|
|
|
$validator = parent::getValidatorInstance(); |
|
48
|
|
|
|
|
49
|
|
|
$validator->sometimes('site_id', 'bail|required|integer|digits_between:1,10|exists:sites,id', function ($input) { |
|
50
|
|
|
return !$input->new_site_name && $input->site_id; |
|
51
|
|
|
}); |
|
52
|
|
|
|
|
53
|
|
|
$validator->sometimes('location_id', 'bail|required|integer|digits_between:1,10|exists:locations,id', function ($input) { |
|
54
|
|
|
return !$input->new_location_name && $input->location_id; |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
$validator->sometimes('new_site_name', 'bail|required|string|max:75|unique:sites,name', function ($input) { |
|
58
|
|
|
return !$input->site_id; |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
$validator->sometimes('new_location_name', 'bail|required|string|max:75|unique:locations,name', function ($input) { |
|
62
|
|
|
return !$input->location_id; |
|
63
|
|
|
}); |
|
64
|
|
|
|
|
65
|
|
|
return $validator; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get the error messages for the defined validation rules. |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public function messages() |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
'name.required' => 'A device name is required', |
|
77
|
|
|
'site_id.required' => 'A site is required', |
|
78
|
|
|
'new_site_name.required' => 'A site is required', |
|
79
|
|
|
'location_id.required' => 'A location is required', |
|
80
|
|
|
'new_location_name.required' => 'A location is required', |
|
81
|
|
|
'open_time.required' => 'A open time for the device is required', |
|
82
|
|
|
'open_time.date_format:H:i' => 'A open time must be in the format hour:minutes', |
|
83
|
|
|
'close_time.required' => 'A close time for the device is required', |
|
84
|
|
|
'close_time.date_format:H:i' => 'A close time must be in the format hour:minutes', |
|
85
|
|
|
'update_rate.required' => 'A update rate for the device is required', |
|
86
|
|
|
'image_rate.required' => 'A image rate for the device is required', |
|
87
|
|
|
'sensor_rate.required' => 'A sensor rate for the device is required', |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: