|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Requests; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
6
|
|
|
use Illuminate\Support\Facades\Auth; |
|
7
|
|
|
|
|
8
|
|
|
class EditDevice extends FormRequest |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Determine if the user is authorized to make this request. |
|
12
|
|
|
* |
|
13
|
|
|
* @return bool |
|
14
|
|
|
*/ |
|
15
|
|
|
public function authorize() |
|
16
|
|
|
{ |
|
17
|
|
|
return Auth::user()->isUser(); |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Get the validation rules that apply to the request. |
|
22
|
|
|
* |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
public function rules() |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
'name' => 'required|string|max:255', |
|
29
|
|
|
'site' => 'required_without:new_site_name|int|max:255|nullable', |
|
30
|
|
|
'new_site_name' => 'required_without:site|unique:sites,name|nullable', |
|
31
|
|
|
'location' => 'required_without:new_location_name|int|max:255|nullable', |
|
32
|
|
|
'new_location_name' => 'required_without:location|unique:locations,name|string|max:255|nullable', |
|
33
|
|
|
'open_time' => 'required|date_format:H:i', |
|
34
|
|
|
'close_time' => 'required|date_format:H:i', |
|
35
|
|
|
'update_rate' => 'required|int|max:255', |
|
36
|
|
|
'image_rate' => 'required|int|max:255', |
|
37
|
|
|
'sensor_rate' => 'required|int|max:255', |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the error messages for the defined validation rules. |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function messages() |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
'name.required' => 'A device name is required', |
|
50
|
|
|
'open_time.required' => 'A open time for the device is required', |
|
51
|
|
|
'open_time.date_format:H:i' => 'A open time most be in the format hour:minutes', |
|
52
|
|
|
'close_time.required' => 'A close time for the device is required', |
|
53
|
|
|
'close_time.date_format:H:i' => 'A close time most be in the format hour:minutes', |
|
54
|
|
|
'update_rate.required' => 'A update rate for the device is required', |
|
55
|
|
|
'image_rate.required' => 'A image rate for the device is required', |
|
56
|
|
|
'sensor_rate.required' => 'A sensor rate for the device is required', |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
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: