File tree Expand file tree Collapse file tree 2 files changed +90
-5
lines changed Expand file tree Collapse file tree 2 files changed +90
-5
lines changed Original file line number Diff line number Diff line change 1
- php-array-forms
1
+ #php-array-forms
2
+ A library that allows you to create HTML forms using PHP Arrays. The project was inspired by Titan Framework and uses the same format for generating elements
3
+ ##INSTALLATION
4
+
5
+ ###Composer
6
+ ` composer require dekyfin/php-array-forms `
7
+
8
+ ###Direct Install
9
+
10
+ ##USAGE
11
+ -Include the ` DF\ArrayForm ` class
12
+ --Composer: ` require_once "vendor/autoload.php" `
13
+ --Direct Install: ` require_once "path/to/ArrayForm.php" `
14
+
15
+ ###Example
16
+ ```
17
+ #Attributes to be used for the form
18
+ $formData = [
19
+ "action" => "/path/to/form/processor.php",
20
+ "method" => "post",
21
+ "class" => "my-special-form",
22
+ "id" => "myForm",
23
+ "display" => "table"
24
+ ];
25
+ $elements = [
26
+ [
27
+ "id" => "email",
28
+ "name" => "Email",
29
+ "type" => "email"
30
+ ],
31
+ [
32
+ "id" => "pass",
33
+ "name" => "Password",
34
+ "type" => "number",
35
+ "step" => "0.01",
36
+ "min" => "3",
37
+ ],
38
+ [
39
+ "id" => "amount",
40
+ "name" => "Amount",
41
+ "type" => "number",
42
+ "step" => "0.01",
43
+ "min" => "3",
44
+ ],
45
+ [
46
+ "id" => "payment[method]",
47
+ "name" => "Payment Method",
48
+ "type" => "select",
49
+ "options" => ["true", "false"],
50
+ ]
51
+ ];
52
+
53
+
54
+ $form = DF\ArrayForm( $formData, $elements );
55
+ $html = $form->$build();
56
+
57
+ echo $html
58
+ ```
59
+ ##OPTIONS
60
+
61
+ ##formData
62
+
63
+ ##elements
64
+
Original file line number Diff line number Diff line change @@ -19,10 +19,16 @@ class ArrayForm {
19
19
protected $ formNo = 0 ;
20
20
protected $ id ;
21
21
22
- public function __construct ( $ formData , $ elements ){
23
- $ this ->formData = $ formData ;
22
+ public function __construct ( Array $ formData , Array $ elements ){
23
+ $ this ->formData = array_merge (
24
+ [
25
+ "method " => "post " ,
26
+ "display " => "table "
27
+ ],
28
+ $ formData
29
+ );
24
30
$ this ->elements = $ elements ;
25
- $ this ->id = rand (9 ,1000 );
31
+ $ this ->id = isset ( $ formData [ " id " ] ) ? $ formData [ " id " ] : rand (9 ,1000 );
26
32
}
27
33
28
34
/**
@@ -168,11 +174,27 @@ protected function renderInputs(){
168
174
}
169
175
170
176
protected function printOutput ( $ output ){
177
+
178
+ $ formData = $ this ->formData ;
179
+ $ classes = $ formData ["class " ];
180
+
181
+ unset(
182
+ $ formData ["id " ],
183
+ $ formData ["class " ],
184
+ $ formData ["class " ]
185
+ );
186
+
187
+ foreach ( $ formData as $ attr =>$ val ){
188
+
189
+ $ val = htmlspecialchars ( $ val );
190
+ $ attributes .= "$ attr=' $ val' " ;
191
+ }
192
+
171
193
//HTML wrapper for all inputs
172
194
$ wrapper = ( $ this ->formData ["display " ] == "table " ) ? ["<table class='DFForm-table'> " ,"</table> " ] : ["" ,"" ];
173
195
174
196
return "
175
- <form id='DFF $ this ->id - $ this ->formNo ' class='DFForm' >
197
+ <form id='DFF $ this ->id - $ this ->formNo ' class='DFForm $ class ' $ attributes >
176
198
$ wrapper [0 ] $ output $ wrapper [1 ]
177
199
</form> " ;
178
200
}
You can’t perform that action at this time.
0 commit comments