Skip to content

Commit 4a33200

Browse files
committed
Added support for form attributes
Modified readme
1 parent e0afdf4 commit 4a33200

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,64 @@
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+

src/DF/ArrayForm.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ class ArrayForm {
1919
protected $formNo = 0;
2020
protected $id;
2121

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+
);
2430
$this->elements = $elements;
25-
$this->id = rand (9,1000);
31+
$this->id = isset( $formData["id"] ) ? $formData["id"] : rand (9,1000);
2632
}
2733

2834
/**
@@ -168,11 +174,27 @@ protected function renderInputs(){
168174
}
169175

170176
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+
171193
//HTML wrapper for all inputs
172194
$wrapper = ( $this->formData["display"] == "table" ) ? ["<table class='DFForm-table'>","</table>"] : ["",""];
173195

174196
return "
175-
<form id='DFF$this->id-$this->formNo' class='DFForm'>
197+
<form id='DFF$this->id-$this->formNo' class='DFForm $class' $attributes>
176198
$wrapper[0] $output $wrapper[1]
177199
</form>";
178200
}

0 commit comments

Comments
 (0)