Skip to content

Commit 01c66c4

Browse files
authored
Update test.md
1 parent 1680550 commit 01c66c4

File tree

1 file changed

+320
-2
lines changed

1 file changed

+320
-2
lines changed

test.md

Lines changed: 320 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#####BismillahHirahManNirRaheem
1+
#####BismillahHirahManNirRaheem#########
22

33
#JSON-PHP Form Handler & Data-validator
44
---------------------------------------
@@ -18,7 +18,7 @@ It is very easy to integrate JSON-PHP Handler & Data-validator in your website/w
1818
"Gender":["Male","Female"]
1919
}'); //json_form() accepts JSON form format as argument
2020

21-
##Json Form Structure
21+
##Json Form Structure##
2222
In **JSON-PHP Handler & Data-validator** a specialized Json form structure if provided during the declariation of object in the parameter or using `json_string($string)` function.
2323

2424
The name part of json string defines the name attribte of `<input>` and the value defines its property (type attribute). The form is parsed according to the property. We can use JSON Property Generator to generate form rules.
@@ -119,3 +119,321 @@ The name part of json string defines the name attribte of `<input>` and the valu
119119
</table>
120120

121121
You can create your own properties.
122+
###Methods/Functionalities of json_from object
123+
<table class="tableblock frame-all grid-all spread">
124+
<colgroup>
125+
<col style="width: 33.3333%;">
126+
<col style="width: 33.3333%;">
127+
<col style="width: 33.3334%;">
128+
</colgroup>
129+
<thead>
130+
<tr>
131+
<th class="tableblock halign-left valign-top">Methods</th>
132+
<th class="tableblock halign-left valign-top">Uses</th>
133+
<th class="tableblock halign-left valign-top">Arguments</th>
134+
</tr>
135+
</thead>
136+
<tbody>
137+
<tr>
138+
<td class="tableblock halign-left valign-top">
139+
<p class="tableblock"><code>json_string($json_str)</code></p>
140+
</td>
141+
<td class="tableblock halign-left valign-top">
142+
<p class="tableblock">It is used to update the basic JSON format of forum.</p>
143+
</td>
144+
<td class="tableblock halign-left valign-top">
145+
<p class="tableblock">It requires a valid JSON format of form template. For more see
146+
examples below.</p>
147+
</td>
148+
</tr>
149+
<tr>
150+
<td class="tableblock halign-left valign-top">
151+
<p class="tableblock"><code>json_array()</code></p>
152+
</td>
153+
<td class="tableblock halign-left valign-top">
154+
<p class="tableblock">It returns the JSON format in plane JSON String or as Associative
155+
array. Depending on argument passed.</p>
156+
</td>
157+
<td class="tableblock halign-left valign-top">
158+
<p class="tableblock">By default return data as array is <code>true</code>, On passing
159+
<code>false</code> plan JSON string will be returned.</p>
160+
</td>
161+
</tr>
162+
<tr>
163+
<td class="tableblock halign-left valign-top">
164+
<p class="tableblock"><code>json_resultant($json_str)</code></p>
165+
</td>
166+
<td class="tableblock halign-left valign-top">
167+
<p class="tableblock">Updates the from result captured from POST/GET requests.</p>
168+
</td>
169+
<td class="tableblock halign-left valign-top">
170+
<p class="tableblock">Requires valid JSON Structure.</p>
171+
</td>
172+
</tr>
173+
<tr>
174+
<td class="tableblock halign-left valign-top">
175+
<p class="tableblock"><code>new_form($template_fn,$bool_echo)</code></p>
176+
</td>
177+
<td class="tableblock halign-left valign-top">
178+
<p class="tableblock">It generates desired HTML code for form to be displayed on the
179+
website.</p>
180+
</td>
181+
<td class="tableblock halign-left valign-top">
182+
<p class="tableblock">It need two arguments: i) <code>template_function</code> it is
183+
the name of template function created by users to provide html
184+
form template, It needs three parameters <code>key</code>,<code>type</code> and <code>value</code>. For more see examples below.
185+
</p>
186+
<p class="tableblock"> ii) <code>$bool_echo</code>: if you wish to pass the html code
187+
to a variable you can set <code>bool_echo</code> to <strong>false</strong>.</p>
188+
</td>
189+
</tr>
190+
<tr>
191+
<td class="tableblock halign-left valign-top">
192+
<p class="tableblock"><code>update_form($template_fn,$values,$bool_echo)</code></p>
193+
</td>
194+
<td class="tableblock halign-left valign-top">
195+
<p class="tableblock">This function is used to generate html code for forms who we want
196+
to update</p>
197+
</td>
198+
<td class="tableblock halign-left valign-top">
199+
<p class="tableblock">It requires same arguments as new_form method excepted one additional
200+
argument <code>$value</code>. <code>$value</code> is the stored
201+
resultant stroed before. It needs to be passed as assoc array.</p>
202+
</td>
203+
</tr>
204+
<tr>
205+
<td class="tableblock halign-left valign-top">
206+
<p class="tableblock"><code>json_handle_request($request_type)</code></p>
207+
</td>
208+
<td class="tableblock halign-left valign-top">
209+
<p class="tableblock">It process and validates the form submitted data. It returns the
210+
processed data as array.</p>
211+
</td>
212+
<td class="tableblock halign-left valign-top">
213+
<p class="tableblock">It accepets type of request as argument. Default is POST Request.</p>
214+
</td>
215+
</tr>
216+
<tr>
217+
<td class="tableblock halign-left valign-top">
218+
<p class="tableblock"><code>get_json_request($bool_is_array)</code></p>
219+
</td>
220+
<td class="tableblock halign-left valign-top">
221+
<p class="tableblock">It returns processed data of json_handle_request()</p>
222+
</td>
223+
<td class="tableblock halign-left valign-top">
224+
<p class="tableblock">By default return data as array is <code>true</code>, On passing
225+
<code>false</code> plan JSON string will be returned.</p>
226+
</td>
227+
</tr>
228+
<tr>
229+
<td class="tableblock halign-left valign-top">
230+
<p class="tableblock"><code>json_secureRule($rule)</code></p>
231+
</td>
232+
<td class="tableblock halign-left valign-top">
233+
<p class="tableblock">It defines the rules for validation during <code>json_handle_request()</code></p>
234+
</td>
235+
<td class="tableblock halign-left valign-top">
236+
<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
237+
see Validation Rules</p>
238+
</td>
239+
</tr>
240+
<tr>
241+
<td class="tableblock halign-left valign-top">
242+
<p class="tableblock"><code>get_json_secureRule($bool_is_array)</code></p>
243+
</td>
244+
<td class="tableblock halign-left valign-top">
245+
<p class="tableblock">Returns validation rules.</p>
246+
</td>
247+
<td class="tableblock halign-left valign-top">
248+
<p class="tableblock">By default return data as array is <code>true</code>, On passing
249+
<code>false</code> plan JSON string will be returned.</p>
250+
</td>
251+
</tr>
252+
<tr>
253+
<td class="tableblock halign-left valign-top">
254+
<p class="tableblock"><code>isJson($string)</code></p>
255+
</td>
256+
<td class="tableblock halign-left valign-top">
257+
<p class="tableblock">Used to check if gives string is json</p>
258+
</td>
259+
<td class="tableblock halign-left valign-top">
260+
<p class="tableblock"><code>JSON</code> formatted string,</p>
261+
</td>
262+
</tr>
263+
<tr>
264+
<td class="tableblock halign-left valign-top">
265+
<p class="tableblock"><code>save_file($Key, $exts )</code></p>
266+
</td>
267+
<td class="tableblock halign-left valign-top">
268+
<p class="tableblock">Used to save file</p>
269+
</td>
270+
<td class="tableblock halign-left valign-top">
271+
<p class="tableblock">It requires two parameters one is the <code>&lt;input&gt;</code> name as <code>$key</code> and second mime extension passed as
272+
array.
273+
</p>
274+
<p class="tableblock">The file location is defined by <code>$obj_name&#8594;json_upload_dir</code></p>
275+
</td>
276+
</tr>
277+
</tbody>
278+
</table>
279+
280+
###Helper function###
281+
282+
Before moving ahead lets first learn what an helper function 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.
283+
284+
JSON-PHP Handler & Data-validator 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 JSON-PHP Handler & Data-validator's methods.
285+
286+
- Template Function
287+
- Validation Function
288+
289+
###Template Function
290+
291+
It is an user defined function which provides HTML template structure for table. It need to have four parameters: name type value and result (for <options/> input). It doesn’t return anything but it prints directly html code during execution.
292+
293+
Example
294+
295+
required_once __DIR__.'/json_form_generator/json_form_class.php'
296+
$form = new json_form(...);
297+
298+
//Drop a new form by using form_template_ex as helper function
299+
$form = $json->new_form("form_template_ex",true);
300+
301+
302+
//template function begins
303+
function form_template_ex($name,$type,$value,$res = null) {
304+
305+
if ($type == "String" || $type == 'string') {
306+
307+
echo "<input Type='text' name='$name' id='$name' placeholder='$name' value='$res'/><br/>";
308+
309+
} else if ($type == "group_caption") {
310+
311+
echo "<label><b>$name</b></label><br/>";
312+
313+
} else if ($type == 'bool') {
314+
315+
echo "<h1>Bool Option Here</h1>";
316+
317+
} else if ($type == 'option') {
318+
319+
$val_new = null;
320+
foreach ($value as $k) {
321+
322+
$val_new .= "<option value='$k'>$k</option>";
323+
}
324+
325+
echo <<< EOD
326+
<div class="form-group">
327+
<label for="input_$name">$name</label>
328+
<select name="$name" id="input_$name" required>
329+
<option value=''>Select $name</option>
330+
$val_new
331+
</select>
332+
</div>
333+
EOD;
334+
} else if ($type == 'img' || $type == 'docs') {
335+
336+
echo "<label>$name</label><input Type='file' name='$name' id='$name' /><br/>";
337+
}
338+
}
339+
340+
###Validation Function###
341+
342+
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.
343+
344+
Example
345+
346+
required_once __DIR__.'/json_form_generator/json_form_class.php'
347+
$form = new json_form(...);
348+
349+
//Drop a new form
350+
$form = $json->new_form(... ,true);
351+
352+
//adding validation rule.
353+
$json->json_secureRule('{
354+
"main_rule":["func:g1"]
355+
}');
356+
357+
function g1($data) {
358+
return "<h1>$data</h1>";
359+
}
360+
361+
###Validation Rules###
362+
363+
Validation rules verify that the data a user enters in a record meets the standards you specify before the user can save the record. JSON-PHP Handler & Data-validator provides a handy way to validate data. The rules are passed in json format through json_secureRule($rule) function. The name part of JSON string should be the input name of form element and the rule should be passed as a value to it in JSON-ARRAY [] format. main_rule is the global rule for all inputs. You can use validation rules for normally submitted form also.
364+
365+
The valid rules of JSON-PHP Handler & Data-validator are:
366+
<table class="tableblock frame-all grid-all spread">
367+
<colgroup>
368+
<col style="width: 50%;">
369+
<col style="width: 50%;">
370+
</colgroup>
371+
<thead>
372+
<tr>
373+
<th class="tableblock halign-left valign-top">Rule</th>
374+
<th class="tableblock halign-left valign-top">Definition</th>
375+
</tr>
376+
</thead>
377+
<tbody>
378+
<tr>
379+
<td class="tableblock halign-left valign-top">
380+
<p class="tableblock">min:number</p>
381+
</td>
382+
<td class="tableblock halign-left valign-top">
383+
<p class="tableblock">Defines minimum characters a string can have.</p>
384+
</td>
385+
</tr>
386+
<tr>
387+
<td class="tableblock halign-left valign-top">
388+
<p class="tableblock">max:number</p>
389+
</td>
390+
<td class="tableblock halign-left valign-top">
391+
<p class="tableblock">Defines maximum characters a string can have.</p>
392+
</td>
393+
</tr>
394+
<tr>
395+
<td class="tableblock halign-left valign-top">
396+
<p class="tableblock">func:function_name</p>
397+
</td>
398+
<td class="tableblock halign-left valign-top">
399+
<p class="tableblock">Defines validation helper function</p>
400+
</td>
401+
</tr>
402+
<tr>
403+
<td class="tableblock halign-left valign-top">
404+
<p class="tableblock">filter:filter_name</p>
405+
</td>
406+
<td class="tableblock halign-left valign-top">
407+
<p class="tableblock">Defines filtration rule. For filtration rules see <a href="https://www.w3schools.com/php/php_ref_filter.asp"
408+
class="bare">https://www.w3schools.com/php/php_ref_filter.asp</a></p>
409+
</td>
410+
</tr>
411+
<tr>
412+
<td class="tableblock halign-left valign-top">
413+
<p class="tableblock">preg:preg_rule</p>
414+
</td>
415+
<td class="tableblock halign-left valign-top">
416+
<p class="tableblock">Defines preg rule for validation.</p>
417+
</td>
418+
</tr>
419+
</tbody>
420+
</table>
421+
422+
Example of Validation rules are as follows:
423+
424+
$json = new json_form('{
425+
"Data": {
426+
"Type": "String",
427+
"email": "String",
428+
}}');
429+
430+
//defing validation rule
431+
$json->json_secureRule('{
432+
"main_rule":["max:255","min:1"],
433+
"email": ["filter:FILTER_VALIDATE_EMAIL"]
434+
}');
435+
436+
//dumping the response
437+
var_dump($json->json_handle_request());
438+
439+
Developed By: **Abrar Ajaz Wani** (abhaywani114@gmail.com)

0 commit comments

Comments
 (0)