Skip to content

Commit

Permalink
Added support for form attributes
Browse files Browse the repository at this point in the history
Modified readme
  • Loading branch information
dekyfin committed Jan 23, 2018
1 parent e0afdf4 commit 4a33200
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
65 changes: 64 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
php-array-forms
#php-array-forms
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
##INSTALLATION

###Composer
`composer require dekyfin/php-array-forms`

###Direct Install

##USAGE
-Include the `DF\ArrayForm` class
--Composer: `require_once "vendor/autoload.php"`
--Direct Install: `require_once "path/to/ArrayForm.php"`

###Example
```
#Attributes to be used for the form
$formData = [
"action" => "/path/to/form/processor.php",
"method" => "post",
"class" => "my-special-form",
"id" => "myForm",
"display" => "table"
];
$elements = [
[
"id" => "email",
"name" => "Email",
"type" => "email"
],
[
"id" => "pass",
"name" => "Password",
"type" => "number",
"step" => "0.01",
"min" => "3",
],
[
"id" => "amount",
"name" => "Amount",
"type" => "number",
"step" => "0.01",
"min" => "3",
],
[
"id" => "payment[method]",
"name" => "Payment Method",
"type" => "select",
"options" => ["true", "false"],
]
];
$form = DF\ArrayForm( $formData, $elements );
$html = $form->$build();
echo $html
```
##OPTIONS

##formData

##elements

30 changes: 26 additions & 4 deletions src/DF/ArrayForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ class ArrayForm {
protected $formNo = 0;
protected $id;

public function __construct( $formData, $elements ){
$this->formData = $formData;
public function __construct( Array $formData, Array $elements ){
$this->formData = array_merge(
[
"method" => "post",
"display" => "table"
],
$formData
);
$this->elements = $elements;
$this->id = rand (9,1000);
$this->id = isset( $formData["id"] ) ? $formData["id"] : rand (9,1000);
}

/**
Expand Down Expand Up @@ -168,11 +174,27 @@ protected function renderInputs(){
}

protected function printOutput( $output ){

$formData = $this->formData;
$classes = $formData["class"];

unset(
$formData["id"],
$formData["class"],
$formData["class"]
);

foreach( $formData as $attr=>$val ){

$val = htmlspecialchars( $val );
$attributes .= "$attr='$val' ";
}

//HTML wrapper for all inputs
$wrapper = ( $this->formData["display"] == "table" ) ? ["<table class='DFForm-table'>","</table>"] : ["",""];

return "
<form id='DFF$this->id-$this->formNo' class='DFForm'>
<form id='DFF$this->id-$this->formNo' class='DFForm $class' $attributes>
$wrapper[0] $output $wrapper[1]
</form>";
}
Expand Down

0 comments on commit 4a33200

Please sign in to comment.