-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.php
96 lines (90 loc) · 3.22 KB
/
handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/*
|---------------------------------------------------------------------------
| Best Practices for submitting and validation
|---------------------------------------------------------------------------
|
| This file validates and stores the block form input in the database.
|
*/
/*
|--------------------------------------------------------------------------|
| NOTE: |
| Everything should be handled within the function handleLinkType(). |
| It replaces the backend controller and ensures data is processed and |
| stored correctly. |
|--------------------------------------------------------------------------|
*/
/**
* Function Structure:
*
* function handleLinkType($request, $linkType) {
* $rules = [];
* $linkData = [];
*
* return ['rules' => $rules, 'linkData' => $linkData];
* }
*
* 1. Accessing Submitted Data
* - You can access any submitted value via the $request variable.
* - Example: If the form field has name="my_value", you can access it with:
* $request->my_value
* - The same name should be used when validating or storing the input.
*
* 2. Validating User Input
* - Always validate user input to ensure it's in the expected format.
* - It is recommended to set maximum input lengths.
* - It is not recommended storing large values or files this way.
*
* 3. Storing Values
* - After validation succeeds, values can be stored in the database.
* - Add any value you want to save to the $linkData array.
* - Predefined values include 'title' and 'link' (can be left empty).
*
* Example:
* $linkData = [
* 'title' => $request->title,
* 'link' => $request->link
* ];
*
* 4. Storing Custom Values
* - You can store any amount of custom values in $linkData. These values
* can be named however you like.
*
* Example:
* $linkData = [
* 'title' => $request->title,
* 'custom_value_1' => $request->something,
* 'custom_value_2' => 'Some Text',
* 'custom_value_3' => $some_var,
* // Add more as needed
* ...
* ];
*
* 5. Reusing Variables
* - All stored variables can be later accessed on the edit and display pages.
*/
function handleLinkType($request, $linkType) {
// Define validation rules
// See: https://laravel.com/docs/11.x/validation#available-validation-rules
$rules = [
'title' => [
'required',
'string',
'max:255',
'regex:/^[a-zA-Z0-9._-]+$/',
],
'snipped' => [
'required',
'string',
'max:50000',
],
];
// Prepare the link data
$linkData = [
'title' => $request->title,
'snipped' => $request->snipped,
'custom_icon' => 'fa-solid fa-code',
];
return ['rules' => $rules, 'linkData' => $linkData];
}