Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created September 20, 2012 14:31
Show Gist options
  • Save xeoncross/3756281 to your computer and use it in GitHub Desktop.
Save xeoncross/3756281 to your computer and use it in GitHub Desktop.
PHP Form Builder
<?php
function assoc_array_map($cb, $arr) {
$new_array = array();
foreach ($arr as $key => $value) {
array_push($new_array, $cb($key, $value));
}
return $new_array;
}
function is_assoc($arr) {
return array_keys($arr) !== range(0, count($arr) - 1);
}
<?php
require "form.php";
form_builder("/test", function() {
?>
<legend>Example</legend>
<?php
label("Username");
text_field("username");
label("Password");
password_field("password");
checkbox_field("tos", "Check if you understand the terms of service", true);
label("Message", "message_body");
textarea_field("message_body");
select_field("preference", function() {
select_options(array(1 => "Category 1", 2 => "Category 2", 3 => "Category 3"));
});
radio_field("service", array("Full time", "Part time", "Whenever"));
radio_field("category", array(1 => "Category 1", 2 => "Category 2", 3 => "Category 3"));
hidden_field("id", 54);
submit_button("Submit");
});
?>
<?php
require "array.php";
require "string.php";
function form_builder($arg, $block=null) {
$options = array();
if (is_string($arg)) {
$options["action"] = $arg;
$options["method"] = "post";
} else {
$options = $arg;
}
// form open
echo "<form " . html_options($options) . ">";
// form body
if (is_callable($block)) {
$block();
}
// form close
echo "</form>";
}
function label($text, $for=NULL) {
if ($for === NULL) {
$for = snake_case($text);
}
echo '<label for="' . $for . '">' . $text . '</label>';
}
function text_field($args) {
$options = array();
if (is_string($args)) {
$options["name"] = $args;
$options["id"] = $args;
$options["type"] = "text";
} else {
$options = $args;
}
echo '<input ' . html_options($options) . '>';
}
function password_field($args) {
$options = array();
if (is_string($args)) {
$options["name"] = $args;
$options["id"] = $args;
} else {
$options = $args;
}
$options["type"] = "password";
echo '<input ' . html_options($options) . '>';
}
function hidden_field($args, $value=NULL) {
$options = array();
if (is_string($args) && $value !== NULL) {
$options["name"] = $args;
$options["id"] = $args;
$options["value"] = $value;
} else {
$options = $args;
}
$options["type"] = "hidden";
echo '<input ' . html_options($options) . '>';
}
function checkbox_field($args, $text, $checked=false) {
$options = array();
if (is_string($args)) {
$options["name"] = $args;
$options["id"] = $args;
if ($checked) {
$options["checked"] = "checked";
}
} else {
$options = $args;
}
$options["type"] = "checkbox";
// label open
echo '<label class="checkbox">';
echo '<input ' . html_options($options) . '> ';
echo $text;
echo '</label>';
}
function radio_field($args, $collection=NULL) {
$options = array();
if (is_string($args)) {
$options["name"] = $args;
} else {
$options = $args;
}
$options["type"] = "radio";
if (is_array($collection)) {
if (is_assoc($collection)) {
foreach($collection as $value => $text) {
echo '<label>';
echo '<input ' . html_options($options) . ' value="' . $value . '"> ';
echo $text;
echo '</label>';
}
} else {
foreach ($collection as $text) {
echo '<label>';
echo '<input ' . html_options($options) . ' value="' . $text . '"> ';
echo $text;
echo '</label>';
}
}
} else {
echo '<label>';
echo '<input ' . html_options($options) . '>';
echo $collection;
echo '</label>';
}
}
function select_field($args, $block=NULL) {
$options = array();
if (is_string($args)) {
$options["name"] = $args;
$options["id"] = $args;
} else {
$options = $args;
}
// select open
echo '<select ' . html_options($options) . '>';
// select body
if (is_callable($block)) {
$block();
}
// select close
echo '</select>';
}
function select_options($collection) {
if (is_assoc($collection)) {
assoc_array_map(function($value, $text) {
echo '<option value="' . $value . '">' . $text . '</option>';
}, $collection);
} else {
array_map(function($text) {
echo '<option>' . $text . '</option>';
}, $collection);
}
}
function textarea_field($args, $value=NULL) {
$options = array();
if (is_string($args)) {
$options["name"] = $args;
} else {
$options = $args;
}
// textarea open
echo '<textarea ' . html_options($options) . '>';
// conditional value
if (is_string($value)) { echo $value; }
// close textarea
echo '</textarea>';
}
function submit_button($args) {
$options = array();
if (is_string($args)) {
$options["name"] = snake_case($args);
$options["id"] = snake_case($args);
$options["value"] = $args;
$options["class"] = "btn";
} else {
$options = $args;
}
$options["type"] = "submit";
echo '<input ' . html_options($options) . '>';
}
function html_options($opts) {
$mapped = assoc_array_map(function($attr, $value) {
return $attr . '="' . $value . '"';
}, $opts);
return implode(" ", $mapped);
}
<form action="/test" method="post">
<legend>Example</legend>
<label for="username">Username</label>
<input name="username" id="username" type="text">
<label for="password">Password</label>
<input name="password" id="password" type="password">
<label class="checkbox"><input name="tos" id="tos" checked="checked" type="checkbox"> Check if you understand the terms of service</label>
<label for="message_body">Message</label>
<textarea name="message_body"></textarea>
<select name="preference" id="preference">
<option value="1">Category 1</option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
</select>
<label><input name="service" type="radio" value="Full time"> Full time</label>
<label><input name="service" type="radio" value="Part time"> Part time</label>
<label><input name="service" type="radio" value="Whenever"> Whenever</label>
<label><input name="category" type="radio" value="1"> Category 1</label>
<label><input name="category" type="radio" value="2"> Category 2</label>
<label><input name="category" type="radio" value="3"> Category 3</label>
<input name="id" id="id" value="54" type="hidden">
<input name="submit" id="submit" value="Submit" class="btn" type="submit">
</form>
<?php
function snake_case($str) {
return strtolower(str_replace(" ", "_", $str));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment