|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Requests\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
7
|
|
|
use Illuminate\Validation\Rule; |
|
8
|
|
|
|
|
9
|
|
|
class TransactionRequest 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 custom messages for validator errors. |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public function messages() |
|
27
|
|
|
{ |
|
28
|
|
|
return [ |
|
29
|
|
|
'account.exists' => 'no entry found for account', |
|
30
|
|
|
'transaction_status.exists' => 'no entry found for transaction_status', |
|
31
|
|
|
'transaction_type.exists' => 'no entry found for transaction_type', |
|
32
|
|
|
'category.exists' => 'no entry found for category', |
|
33
|
|
|
'subcategory.exists' => 'no entry found for subcategory', |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get the validation rules that apply to the request. |
|
39
|
|
|
* |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public function rules() |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
|
|
'transaction_date' => 'nullable|date|date_format:'.Carbon::ATOM, |
|
46
|
|
|
'transaction_status' => 'string|exists:transaction_status,name', |
|
47
|
|
|
'transaction_type' => 'required|string|exists:transaction_types,name', |
|
48
|
|
|
'account' => [ |
|
49
|
|
|
'required', |
|
50
|
|
|
'string', |
|
51
|
|
|
Rule::exists('accounts', 'name')->where('user_id', auth()->user()->id), |
|
|
|
|
|
|
52
|
|
|
], |
|
53
|
|
|
'to_account' => 'sometimes|required|string', |
|
54
|
|
|
'payee' => 'sometimes|required|string', |
|
55
|
|
|
'category' => [ |
|
56
|
|
|
'required', |
|
57
|
|
|
'string', |
|
58
|
|
|
Rule::exists('categories', 'name')->where('user_id', auth()->user()->id)->whereNull('parent_id'), |
|
59
|
|
|
], |
|
60
|
|
|
'subcategory' => [ |
|
61
|
|
|
'string', |
|
62
|
|
|
Rule::exists('categories', 'name')->where('user_id', auth()->user()->id)->whereNotNull('parent_id'), |
|
63
|
|
|
], |
|
64
|
|
|
'amount' => 'required|numeric|min:0|max:999999', |
|
65
|
|
|
'notes' => 'string|nullable', |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: