Skip to content

Commit a08f0b5

Browse files
committed
Several bug fixes and improvements
1 parent c5b015a commit a08f0b5

File tree

2 files changed

+55
-28
lines changed

2 files changed

+55
-28
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# php-array-forms
1+
# PHP ARRAY FORMS
22
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
33
## INSTALLATION
44

@@ -27,13 +27,13 @@ $elements = [
2727
"id" => "email",
2828
"name" => "Email",
2929
"type" => "email"
30+
"required" => true,
3031
],
3132
[
3233
"id" => "pass",
3334
"name" => "Password",
34-
"type" => "number",
35-
"step" => "0.01",
36-
"min" => "3",
35+
"type" => "password",
36+
"required" => true,
3737
],
3838
[
3939
"id" => "amount",

src/DF/ArrayForm.php

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public function __construct( Array $formData, Array $elements ){
2323
$this->formData = array_merge(
2424
[
2525
"method" => "post",
26-
"display" => "table"
26+
"display" => "table",
27+
"action" => ""
2728
],
2829
$formData
2930
);
@@ -65,15 +66,16 @@ protected function renderInputs(){
6566
foreach ($this->elements as $element ) {
6667

6768
//Values to be reset for each input
68-
$opts = $input = $label = $attributes = "";
69+
$opts = $input = $hidden = $label = $attributes = "";
6970

7071

7172
//Extract key data from attributes
7273
$id = htmlspecialchars( $element["id"] );
7374
$name = htmlspecialchars( $element["name"] );
7475
$options = $element["options"] ;
76+
$wrapper = isset( $element["wrapper"] ) ? $element["wrapper"] : [] ;
7577
$default = htmlspecialchars( $element["default"] );
76-
$type = htmlspecialchars( $element["type"] );
78+
$type = isset($element["type"]) ? htmlspecialchars( $element["type"] ) : "text";
7779

7880

7981

@@ -82,6 +84,7 @@ protected function renderInputs(){
8284
$element["name"],
8385
$element["options"],
8486
$element["type"],
87+
$element["wrapper"],
8588
$element["default"]
8689
);
8790

@@ -94,20 +97,30 @@ protected function renderInputs(){
9497
$val = htmlspecialchars( $val );
9598

9699
if( gettype( $val ) === true ){
97-
$attributes .= "$attr='$attr' ";
100+
$attributes .= "$attr=\"$attr\" ";
98101
}
99102
if( gettype( $val ) === false ){
100103

101104
}
102105
else{
103-
$attributes .= "$attr='$val' ";
106+
$attributes .= "$attr=\"$val\" ";
104107
}
105108
}
106109

110+
//Create Attributes
111+
$wrapper_attr = "";
112+
foreach( $wrapper as $attr=> $val ){
113+
114+
$val = htmlspecialchars( $val );
115+
$wrapper_attr .= "$attr=\"$val\" ";
116+
}
117+
107118
//Create options
119+
$label = "<label for=\"$inputID\">$name</label>";
120+
108121
switch ( $type ) {
109-
case 'textarea':
110-
$input = "<textarea id='$inputID' name='$id' value='$default' $attributes></textarea>";
122+
case "textarea":
123+
$input = "<textarea id=\"$inputID\" name=\"$id\" value=\"$default\" $attributes></textarea>";
111124
break;
112125
case "select":
113126
foreach( $options as $key => $val ){
@@ -116,52 +129,62 @@ protected function renderInputs(){
116129
}
117130
else{
118131
$key = htmlspecialchars( $key );
119-
$value = "value='$key'";
132+
$value = "value=\"$key\"";
120133
}
121134
$opts .= "\n <option $value>$val</option>";
122135
}
123-
$input = "<select id='$inputID' name='$id' $attributes value='$default'>$opts\n </select>";
136+
$input = "<select id=\"$inputID\" name=\"$id\" $attributes value=\"$default\">$opts\n </select>";
124137

125138
break;
126139
case "radio":
127140
case "checkbox":
128141
foreach( $options as $key=>$val ){
129142
if( is_numeric( $key ) ){
130143
$val = htmlspecialchars( $val );
131-
$value = "value='$val'";
144+
$value = "value=\"$val\"";
132145
}
133146
else{
134147
$key = htmlspecialchars( $key );
135-
$value = "value='$key'";
148+
$value = "value=\"$key\"";
136149
}
137-
$input .= "\n<label><input type='$type' name='$id' $attributes $value/>$val</label>";
150+
$input .= "\n<label><input type=\"$type\" name=\"$id\" $attributes $value/>$val</label>";
138151
}
139152

140153
break;
141-
case 'submit':
142-
$input = '<input type="submit" name="' . $id . '" value="' . $name . '">';
143-
$label = '';
154+
case "submit":
155+
$input = "<input type=\"submit\" name=\"$id\" value=\"$name\">";
156+
$label = "";
144157
break;
145-
default:
146-
$input = "<input id='$inputID' type='$type' name='$id' $attributes />";
158+
case "custom":
159+
$input = $element["custom"];
160+
$label = "";
147161
break;
162+
default:
163+
$input = "<input id=\"$inputID\" type=\"$type\" name=\"$id\" $attributes value=\"$default\" />";
164+
165+
}
166+
167+
//Do not display hidden forms
168+
if( $type == "hidden" ){
169+
$hidden .= $input;
170+
continue;
148171
}
149172

150173
switch ( $this->formData["display"] ){
151174

152175
case "table":
153176
$output .=<<<INPUT
154-
<tr>
155-
<td class="DFForm-input-title"><label for="$inputID">$name</label></td>
177+
<tr $wrapper_attr>
178+
<td class="DFForm-input-title">$label</td>
156179
<td class="DFForm-input-content">$input</td>
157180
</tr>
158181
INPUT;
159182
break;
160183

161184
default:
162185
$output .=<<<INPUT
163-
<div class="DFForm-input">
164-
<div><label for="$inputID">$name</label></div>
186+
<div $wrapper_attr class="DFForm-input-wrap">
187+
$label
165188
<div>$input</div>
166189
</div>
167190
INPUT;
@@ -170,13 +193,16 @@ protected function renderInputs(){
170193

171194
}
172195

173-
return $output;
196+
return [$output, $hidden ];
174197
}
175198

176-
protected function printOutput( $output ){
199+
protected function printOutput( $inputs ){
200+
201+
$output = $inputs[0];
202+
$hidden = $inputs[1];
177203

178204
$formData = $this->formData;
179-
$classes = $formData["class"];
205+
$class = $formData["class"];
180206

181207
unset(
182208
$formData["id"],
@@ -196,6 +222,7 @@ protected function printOutput( $output ){
196222
return "
197223
<form id='DFF$this->id-$this->formNo' class='DFForm $class' $attributes>
198224
$wrapper[0] $output $wrapper[1]
225+
$hidden
199226
</form>";
200227
}
201228
}

0 commit comments

Comments
 (0)