Skip to content

Instantly share code, notes, and snippets.

@ricardoalcocer
Created May 11, 2023 19:09
Show Gist options
  • Save ricardoalcocer/c498c7dece5c3b1420212f254cf3a139 to your computer and use it in GitHub Desktop.
Save ricardoalcocer/c498c7dece5c3b1420212f254cf3a139 to your computer and use it in GitHub Desktop.
PHP FormBuilder
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
/**
* Alco Form Builder.
*
* Build a simple form based on a JSON file and store it as a K/V Store.
*
* @since 1.0.0
*
* @link https://alco.rocks
* @param type $inJSON. Form Definition file as JSON.
* @return type HTML Form.
*/
class FormBuilder {
public $inJSON;
public $formAction;
public $formToken;
function __construct($inJSON,$inToken) {
$this->inJSON = $inJSON;
$this->formToken = $inToken;
$this->formAction = "s.php";
}
function getMeta(){
$parsedForm = json_decode($this->inJSON,true);
return (Object)["title" => $parsedForm['title'],"subtitle" => $parsedForm['subtitle']];
}
public function build() {
$parsedForm = json_decode($this->inJSON,true);
$fieldsHTML = "";
$TOPHTML = <<<END
<form action="{$this->formAction}" method="post">
<input name="token" type="hidden" value="{$this->formToken}">
<div class="card-image has-text-centered">
<figure class="image is-128x128 is-inline-block">
<img class="is-rounded" src="https://avatars.githubusercontent.com/u/64380470?s=200&v=4" alt="iGenApps Logo">
</figure>
</div>
<h1 class="title has-text-centered">{$parsedForm['title']}</h1>
<h2 class="subtitle has-text-centered">{$parsedForm['subtitle']}</h2>
END;
foreach ($parsedForm['data'] as $key => $value) {
switch($value['type']){
case "text":
$fieldsHTML.='<div class="field">';
$fieldsHTML.=' <label class="label">' . $value['label'] . '</label>' . PHP_EOL;
$fieldsHTML.=' <div class="control">' . PHP_EOL;
$fieldsHTML.=' <input class="input" type="text" name="' . $value['name'] . '" placeholder="' . $value['default'] . '">' . PHP_EOL;
$fieldsHTML.=' </div>' . PHP_EOL;
$fieldsHTML.='</div>' . PHP_EOL;
break;
case "email":
$fieldsHTML.='<div class="field">' . PHP_EOL;
$fieldsHTML.=' <label class="label">' . $value['label'] . '</label>' . PHP_EOL;
$fieldsHTML.=' <div class="control">' . PHP_EOL;
$fieldsHTML.=' <input class="input" type="email" name="' . $value['name'] . '" placeholder="' . $value['default'] . '">' . PHP_EOL;
$fieldsHTML.=' </div>' . PHP_EOL;
$fieldsHTML.='</div>' . PHP_EOL;
break;
case "hidden":
$fieldsHTML.='<input class="input" type="hidden" name="' . $value['name'] . '" value="' . $value['default'] . '">' . PHP_EOL;
break;
case "select_single":
$fieldsHTML.='<div class="field">' . PHP_EOL;
$fieldsHTML.=' <label class="label">' . $value['label'] . '</label>' . PHP_EOL;
$fieldsHTML.=' <div class="select">' . PHP_EOL;
$fieldsHTML.=' <select>' . PHP_EOL;
foreach ($value["values"] as $key => $value) {
$fieldsHTML.='<option value="' . $value["value"] . '">' . $value["label"] . '</option>' . PHP_EOL;
}
$fieldsHTML.=' </select>' . PHP_EOL;
$fieldsHTML.=' </div>' . PHP_EOL;
$fieldsHTML.='</div>' . PHP_EOL;
break;
case "radio_group":
$fieldsHTML.='<div class="field">' . PHP_EOL;
$fieldsHTML.=' <label class="label">' . $value['label'] . '</label>' . PHP_EOL;
$fieldsHTML.=' <div class="checkbox">' . PHP_EOL;
foreach ($value["values"] as $key => $element) {
$fieldsHTML.='<input type="radio" name="' . $value['name'] . '" value="' . $element["value"] . '">&nbsp;' . $element["label"] . PHP_EOL;
}
$fieldsHTML.=' </div>' . PHP_EOL;
$fieldsHTML.='</div>' . PHP_EOL;
break;
case "checkbox_group":
$fieldsHTML.='<div class="field">' . PHP_EOL;
$fieldsHTML.=' <label class="label">' . $value['label'] . '</label>' . PHP_EOL;
$fieldsHTML.=' <div class="checkbox">' . PHP_EOL;
foreach ($value["values"] as $key => $element) {
$fieldsHTML.='<input type="checkbox" name="' . $value['name'] . '" value="' . $element["value"] . '">&nbsp;' . $element["label"]. PHP_EOL;
}
$fieldsHTML.=' </div>' . PHP_EOL;
$fieldsHTML.='</div>' . PHP_EOL;
break;
case "submit":
$fieldsHTML.='<div class="field">' . PHP_EOL;
$fieldsHTML.=' <div class="field has-text-centered">' . PHP_EOL;
$fieldsHTML.=' <input class="button is-info" type="submit" name="' . $value['label'] . '" value="' . $value['label'] .'">' . PHP_EOL;
$fieldsHTML.=' </div>' . PHP_EOL;
$fieldsHTML.='</div>' . PHP_EOL;
break;
case "textarea":
$fieldsHTML.='<div class="field">';
$fieldsHTML.=' <label class="label">' . $value['label'] . '</label>' . PHP_EOL;
$fieldsHTML.=' <div class="control">' . PHP_EOL;
$fieldsHTML.=' <textarea class="textarea" name="' . $value['name'] . '" placeholder="' . $value['default'] . '"></textarea>' . PHP_EOL;
$fieldsHTML.=' </div>' . PHP_EOL;
$fieldsHTML.='</div>' . PHP_EOL;
}
}
$BOTTOMHTML = "</form>";
return $TOPHTML . $fieldsHTML . $BOTTOMHTML;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment