1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Requests; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
|
8
|
|
|
class InvoiceRequest 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
|
|
|
// only allow updates if the user is logged in |
18
|
|
|
return backpack_auth()->check(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get the validation rules that apply to the request. |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function rules() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
'client_name' => 'required', |
30
|
|
|
'client_idnumber' => 'required', |
31
|
|
|
'client_address' => 'required', |
32
|
|
|
'client_email' => 'required', |
33
|
|
|
'payments' => function ($attribute, $value, $fail) { |
34
|
|
|
$fieldGroups = json_decode($value); |
35
|
|
|
|
36
|
|
|
// allow repeatable field to be empty |
37
|
|
|
if (count($fieldGroups) == 0) { |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// SECOND-LEVEL REPEATABLE VALIDATION |
42
|
|
|
// run through each field group inside the repeatable field |
43
|
|
|
// and run a custom validation for it |
44
|
|
|
foreach ($fieldGroups as $key => $group) { |
45
|
|
|
$fieldGroupValidator = Validator::make((array)$group, ['payment_method' => 'required|string', 'date' => 'required|date', 'value' => 'numeric|required', 'comment' => 'string|nullable', 'responsable_id' => 'nullable',]); |
46
|
|
|
|
47
|
|
|
if ($fieldGroupValidator->fails()) { |
48
|
|
|
return $fail($fieldGroupValidator->errors()->first()); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
}, |
52
|
|
|
'invoiceDetails' => function ($attribute, $value, $fail) { |
53
|
|
|
$fieldGroups = json_decode($value); |
54
|
|
|
|
55
|
|
|
// allow repeatable field to be empty |
56
|
|
|
if (count($fieldGroups) == 0) { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// SECOND-LEVEL REPEATABLE VALIDATION |
61
|
|
|
// run through each field group inside the repeatable field |
62
|
|
|
// and run a custom validation for it |
63
|
|
|
foreach ($fieldGroups as $key => $group) { |
64
|
|
|
$fieldGroupValidator = Validator::make((array)$group, ['product_name' => 'required|string', 'price' => 'numeric|required', 'quantity' => 'numeric|required']); |
65
|
|
|
|
66
|
|
|
if ($fieldGroupValidator->fails()) { |
67
|
|
|
return $fail($fieldGroupValidator->errors()->first()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
}, |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the validation attributes that apply to the request. |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function attributes() |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
// |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the validation messages that apply to the request. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function messages() |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
// |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|