Skip to content

Instantly share code, notes, and snippets.

@smuuf
Created February 14, 2016 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smuuf/b51cecfea3825c687291 to your computer and use it in GitHub Desktop.
Save smuuf/b51cecfea3825c687291 to your computer and use it in GitHub Desktop.
Crude Form class example
<?php
/**
* A very basic crude Form class.
* It can have input controls + will know their values.
* Also will know if it (the form) was itself submitted.
*/
class CrudeForm {
/** @var string Form name for identifying the form. **/
protected $name;
/** @var array Container for form controls' HTML. **/
protected $controls = array();
/** @var bool Was this form rendered? **/
protected $rendered = false;
/**
* Create new form. It is _your_ responsibility not to create two forms
* with the same name
*/
public function __construct(string $name) {
$this->name = sprintf("_crudeform_%s", $name);
}
/**
* Get form name
*/
public function getName() {
return $this->name;
}
/**
* Add textbox to the form.
* Shorthand for self::addInput().
*/
public function addTextbox(string $name, string $label) {
$this->addInput($name, $label, 'text');
return $this;
}
/**
* Add submit button to the form.
* Shorthand for self::addInput().
*/
public function addSubmit(string $name) {
$this->addInput($name, null, 'submit');
return $this;
}
/**
* Add an imput to the form.
*/
public function addInput(string $name, $label, string $type) {
$uniqueName = $this->buildInputName($name);
$this->controls[$name] = sprintf(
"<label for='%s'>%s</label><input type='%s' name='%s'>",
$uniqueName,
$label,
$type,
$uniqueName
);
return $this;
}
/**
* Render the form. Output its final HTML to the page.
*/
public function render() {
if ($this->rendered) {
throw new \LogicException(sprintf("From '%s' has already been rendered.", $this->name));
}
$html = sprintf(
"<form name='%s' method='post'>%s%s</form>",
$this->name,
self::getFormHiddenIdInput(),
implode('<br>', $this->controls)
);
echo $html;
$this->rendered = true;
}
/**
* Was this form submitted during the last request?
*/
public function wasSubmitted() {
return isset($_POST[$this->getHiddenIdName()]);
}
/**
* Return an array containing values of form's controls.
*/
public function getValues() {
$values = array();
// Go through each of this form's controls and check if they have
// any values in the POST data.
foreach ($this->controls as $name => $controlHtml) {
// This control's value will be recognized by its unique name
// in the POST data array.
$uniqueName = $this->buildInputName($name);
if (isset($_POST[$uniqueName])) {
$values[$name] = $_POST[$uniqueName];
}
}
return $values;
}
// Helpers
/**
* Build a page-wide unique name for _this_ input and _this_ form.
*/
protected function buildInputName(string $rawName) {
return sprintf('_crudeform_input_%s', self::hashVariable([
$this->name,
$rawName
]));
}
/**
* Returns the name of the form's hidden identification input.
*/
protected function getHiddenIdName() {
return sprintf('_crudeform_id_%s', md5($this->name));
}
/**
* Returns HTML of the form's hidden ID input.
*/
protected function getFormHiddenIdInput() {
return sprintf(
"<input type='hidden' name='%s'>", $this->getHiddenIdName()
);
}
/**
* Hash whatever is given to this.
*/
protected static function hashVariable() {
return md5(json_encode(func_get_args()));
}
}
<?php
include __DIR__ . "/CrudeForm.php";
// Build all forms
$formOne = new CrudeForm('first_form');
$formOne->addTextbox('first_textbox', 'Your name:');
$formOne->addInput('first_checkbox', 'Are you cool?', 'checkbox');
$formOne->addSubmit('first_submit', 'Test me');
$formTwo = new CrudeForm('second_form');
$formTwo->addTextbox('second_textbox', 'Your second name:');
$formTwo->addInput('second_checkbox', 'Are you really cool?', 'checkbox');
$formTwo->addSubmit('second_submit', 'Test me');
// All under this comment must be called
// after registration of all form's controls.
if ($formOne->wasSubmitted()) {
$submittedForm = $formOne;
} elseif ($formTwo->wasSubmitted()) {
$submittedForm = $formTwo;
}
// If any form was submitted at all
if (!empty($submittedForm)) {
// Get form's values...
if ($submittedValues = $submittedForm->getValues()) {
// ...and echo informatino about the submitted form
echo sprintf("<h2>Form '%s' data</h2>", $submittedForm->getName());
foreach ($submittedValues as $name => $value) {
echo sprintf("<b>%s</b>: %s<br>", $name, print_r($value, true));
}
echo "<hr>";
}
}
?>
<h1>Cool form page title</h1>
<h2>First form</h2>
<div><?php $formOne->render(); ?></div>
<h2>Second form</h2>
<div><?php $formTwo->render(); ?></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment