1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Requests\Dashboard;
|
---|
4 |
|
---|
5 | use App\Models\Department;
|
---|
6 | use App\Models\Document;
|
---|
7 | use Illuminate\Foundation\Http\FormRequest;
|
---|
8 |
|
---|
9 | class DocumentRequest 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 | if ($this->isMethod("patch")) {
|
---|
19 | $document = Document::find($this->route("id"));
|
---|
20 | return auth()->user()->hasPermission("edit_all_documents") || ($document->user->id == auth()->user()->id);
|
---|
21 | }
|
---|
22 |
|
---|
23 | return true;
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Get the validation rules that apply to the request.
|
---|
28 | *
|
---|
29 | * @return array
|
---|
30 | */
|
---|
31 | public function rules()
|
---|
32 | {
|
---|
33 | $rules = [
|
---|
34 | "arch_id" => [
|
---|
35 | "required",
|
---|
36 | function ($attribute, $value, $fail) {
|
---|
37 | $arch_id = $this->request->get('arch_id');
|
---|
38 | $deptId = explode('/', $arch_id)[0];
|
---|
39 | $archNum = explode('/', $arch_id)[1];
|
---|
40 |
|
---|
41 | if (empty($archNum)) {
|
---|
42 | $fail("Please enter documents Archive ID");
|
---|
43 | }
|
---|
44 |
|
---|
45 | if ($deptId !== Department::find($this->request->get('department'))->code) {
|
---|
46 | $fail("Document Archive ID field format is invalid");
|
---|
47 | }
|
---|
48 | }
|
---|
49 | ],
|
---|
50 | "name" => "required|min:10|max:255",
|
---|
51 | "department" => "required|integer|exists:departments,id",
|
---|
52 | "description" => "required|min:30",
|
---|
53 | ];
|
---|
54 |
|
---|
55 | if ($this->isMethod("patch")) {
|
---|
56 | $fileRules = [
|
---|
57 | "file_item.*" => "mimes:jpg,jpeg,png,pdf|max:4096"
|
---|
58 | ];
|
---|
59 | } else {
|
---|
60 | $fileRules = [
|
---|
61 | "file_item.*" => "mimes:jpg,jpeg,png,pdf|max:4096"
|
---|
62 | ];
|
---|
63 | }
|
---|
64 |
|
---|
65 | $rules = array_merge(
|
---|
66 | $rules,
|
---|
67 | $fileRules,
|
---|
68 | );
|
---|
69 |
|
---|
70 | return $rules;
|
---|
71 | }
|
---|
72 | }
|
---|