-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.html
546 lines (523 loc) · 23.8 KB
/
help.html
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 1.5.4">
<title>JSON-PHP Form Handler & Data-validator</title>
<link rel="stylesheet" href="asset/css/asciidoctor.css">
</head>
<body class="article">
<div id="header">
</div>
<div id="content">
<div class="paragraph">
<p>
<div align="center"><b>BismillahHirahManNirRaheem</b></div>
</p>
</div>
<div class="sect1">
<h2 id="_json_php_form_handler_data_validator">JSON-PHP Form Handler & Data-validator</h2>
<div class="sectionbody">
<div class="paragraph">
<p><strong>JSON-PHP Handler & Data-validator</strong> is a php based library
which provides an easy way to:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Generate highly dynamic HTML forms (Using JSON).</p>
</li>
<li>
<p>Validate the HTML form response in the backbend.</p>
</li>
<li>
<p>Store the user data in database in the form of JSON.</p>
</li>
</ul>
</div>
<div class="sect2">
<h3 id="_integration">Integration</h3>
<div class="paragraph">
<p>It is very easy to integrate <strong>JSON-PHP Handler & Data-validator</strong> in your website/webapp. We just have to include the class file and
start to make objects.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-php" data-lang="php">required_once __DIR__.'/json_form_generator/json_form_class.php'
$form = new json_form('{
"Name":"String",
"Gender":["Male","Female"]
}'); //json_form() accepts JSON form format as argument</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_json_form_structure">Json Form Structure</h3>
<div class="paragraph">
<p>In <strong>JSON-PHP Handler & Data-validator</strong> a specialized
Json form structure if provided during the declariation of object in
the parameter or using <code>json_string($string)</code> function.</p>
</div>
<div class="paragraph">
<p>The <code>name</code> part of json string defines the name attribte of
<code><input></code> and the value defines its property (<code>type</code> attribute). The form is parsed according to the property. We can use
JSON Property Generator to generate form rules.</p>
</div>
<div class="paragraph">
<p><code>Warning: <strong>name</strong> should be unique and without spaces is recommended.</code></p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Property</th>
<th class="tableblock halign-left valign-top">Definition</th>
<th class="tableblock halign-left valign-top">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock">String</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It is simple text input type</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">{"username":"String"}</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Object</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">it defines parent label of group of inputs. It can be access by
<code>group_caption</code> label in helper function</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">{
</p>
<p class="tableblock">"User":{
</p>
<p class="tableblock">"name":"String",
</p>
<p class="tableblock">"age":"String"
</p>
<p class="tableblock">,"Country":"String"}
</p>
<p class="tableblock">}
</p>
<p class="tableblock"><code>User is group caption</code></p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Array</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It defines <option> of <select> element. The options
are passed as json array</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">{"Country": [
</p>
<p class="tableblock"> "India",
</p>
<p class="tableblock"> "Pakistan",
</p>
<p class="tableblock"> "Iraq"
</p>
<p class="tableblock">]}</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Bool</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It defines Yes or No</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">{"Interested":"bool"}</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Password</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It defines password type</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">{"pwd":"password"}</p>
</td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>You can create your own properties.</p>
</div>
</div>
<div class="sect2">
<h3 id="_methods_functionalities_of_code_json_from_code_object">Methods/Functionalities of <code>json_from</code> object</h3>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Methods</th>
<th class="tableblock halign-left valign-top">Uses</th>
<th class="tableblock halign-left valign-top">Arguments</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>json_string($json_str)</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It is used to update the basic JSON format of forum.</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It requires a valid JSON format of form template. For more see
examples below.</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>json_array()</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It returns the JSON format in plane JSON String or as Associative
array. Depending on argument passed.</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">By default return data as array is <code>true</code>, On passing
<code>false</code> plan JSON string will be returned.</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>json_resultant($json_str)</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Updates the from result captured from POST/GET requests.</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Requires valid JSON Structure.</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>new_form($template_fn,$bool_echo)</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It generates desired HTML code for form to be displayed on the
website.</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It need two arguments: i) <code>template_function</code> it is
the name of template function created by users to provide html
form template, It needs three parameters <code>key</code>,<code>type</code> and <code>value</code>. For more see examples below.
</p>
<p class="tableblock"> ii) <code>$bool_echo</code>: if you wish to pass the html code
to a variable you can set <code>bool_echo</code> to <strong>false</strong>.</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>update_form($template_fn,$values,$bool_echo)</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">This function is used to generate html code for forms who we want
to update</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It requires same arguments as new_form method excepted one additional
argument <code>$value</code>. <code>$value</code> is the stored
resultant stroed before. It needs to be passed as assoc array.</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>json_handle_request($request_type)</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It process and validates the form submitted data. It returns the
processed data as array.</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It accepets type of request as argument. Default is POST Request.</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>get_json_request($bool_is_array)</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It returns processed data of json_handle_request()</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">By default return data as array is <code>true</code>, On passing
<code>false</code> plan JSON string will be returned.</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>json_secureRule($rule)</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It defines the rules for validation during <code>json_handle_request()</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It accepts rules like <code>min</code> <code>max</code> <code>preg</code> and even helper function to process data efficiently. For more
see Validation Rules</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>get_json_secureRule($bool_is_array)</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Returns validation rules.</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">By default return data as array is <code>true</code>, On passing
<code>false</code> plan JSON string will be returned.</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>isJson($string)</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Used to check if gives string is json</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>JSON</code> formatted string,</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock"><code>save_file($Key, $exts )</code></p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Used to save file</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">It requires two parameters one is the <code><input></code> name as <code>$key</code> and second mime extension passed as
array.
</p>
<p class="tableblock">The file location is defined by <code>$obj_name→json_upload_dir</code></p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="sect2">
<h3 id="_helper_function">Helper function</h3>
<div class="paragraph">
<p>Before moving ahead lets first learn what an <strong>helper function</strong> is? A helper function is a arbitrary function which is called during
data processing to extend the process by running desired operation
on data. It is not the part of Library but it is always user defined
and is called by Library during standard operation.</p>
</div>
<div class="paragraph">
<p><strong>JSON-PHP Handler & Data-validator</strong> often uses helper
function to give developer free hand to manipulate the data according
to their requirement. There are two helper function which are required
by <strong>JSON-PHP Handler & Data-validator</strong>'s methods.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>Template Function</strong></p>
</li>
<li>
<p><strong>Validation Function</strong></p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="_template_function">Template Function</h3>
<div class="paragraph">
<p>It is an user defined function which provides HTML template structure
for table. It need to have four parameters: <code>name</code> <code>type</code> <code>value</code> and <code>result</code> (for <options/> input).
It doesn’t return anything but it prints directly html code during
execution.</p>
</div>
<div class="paragraph">
<p><strong>Example</strong></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-php" data-lang="php">required_once __DIR__.'/json_form_generator/json_form_class.php'
$form = new json_form(...);
//Drop a new form by using form_template_ex as helper function
$form = $json->new_form("form_template_ex",true);
//template function begins
function form_template_ex($name,$type,$value,$res = null) {
if ($type == "String" || $type == 'string') {
echo "<input Type='text' name='$name' id='$name' placeholder='$name' value='$res'/><br/>";
} else if ($type == "group_caption") {
echo "<label><b>$name</b></label><br/>";
} else if ($type == 'bool') {
echo "<h1>Bool Option Here</h1>";
} else if ($type == 'option') {
$val_new = null;
foreach ($value as $k) {
$val_new .= "<option value='$k'>$k</option>";
}
echo <<< EOD
<div class="form-group">
<label for="input_$name">$name</label>
<select name="$name" id="input_$name" required>
<option value=''>Select $name</option>
$val_new
</select>
</div>
EOD;
} else if ($type == 'img' || $type == 'docs') {
echo "<label>$name</label><input Type='file' name='$name' id='$name' /><br/>";
}
}</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_validation_function">Validation Function</h3>
<div class="paragraph">
<p>Validation Function is also a helper function which is used to validate
and modify data submitted by forms. It expectes one parameter and return
the data after modification/validation.</p>
</div>
<div class="paragraph">
<p><strong>Example</strong></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-php" data-lang="php">required_once __DIR__.'/json_form_generator/json_form_class.php'
$form = new json_form(...);
//Drop a new form
$form = $json->new_form(... ,true);
//adding validation rule.
$json->json_secureRule('{
"main_rule":["func:g1"]
}');
function g1($data) {
return "<h1>$data</h1>";
}</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_validation_rules">Validation Rules</h3>
<div class="paragraph">
<p><strong>Validation rules</strong> verify that the data a user enters
in a record meets the standards you specify before the user can save
the record. <strong>JSON-PHP Handler & Data-validator</strong> provides a handy way to validate data. The rules are passed in json
format through <code>json_secureRule($rule)</code> function. The <strong>name</strong> part of JSON string should be the <strong>input name</strong> of form
element and the <strong>rule</strong> should be passed as a value to
it in JSON-ARRAY [] format. <code>main_rule</code> is the global rule
for all inputs. You can use validation rules for normally submitted
form also.</p>
</div>
<div class="paragraph">
<p>The valid rules of <strong>JSON-PHP Handler & Data-validator</strong> are:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Rule</th>
<th class="tableblock halign-left valign-top">Definition</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock">min:number</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Defines minimum characters a string can have.</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock">max:number</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Defines maximum characters a string can have.</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock">func:function_name</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Defines validation helper function</p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock">filter:filter_name</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Defines filtration rule. For filtration rules see <a href="https://www.w3schools.com/php/php_ref_filter.asp"
class="bare">https://www.w3schools.com/php/php_ref_filter.asp</a></p>
</td>
</tr>
<tr>
<td class="tableblock halign-left valign-top">
<p class="tableblock">preg:preg_rule</p>
</td>
<td class="tableblock halign-left valign-top">
<p class="tableblock">Defines preg rule for validation.</p>
</td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Example of Validation rules are as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-php" data-lang="php">$json = new json_form('{
"Data": {
"Type": "String",
"email": "String",
}}');
//defing validation rule
$json->json_secureRule('{
"main_rule":["max:255","min:1"],
"email": ["filter:FILTER_VALIDATE_EMAIL"]
}');
//dumping the response
var_dump($json->json_handle_request());</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>
<div align="center"><b>Developed By: Abrar Ajaz Wani (abhaywani114@gmail.com)</b></div>
</p>
</div>
</div>
</body>
</html>