A simple, lightweight PHP class that makes creating forms easy
This is a small PHP class that makes it easy to build and output forms as HTML or XHTML. Forms are tedious and can be difficult to build just right. Also, there are so many different option possible that it's easy to forget what you can do with them., (*1)
I tried to balance ease-of-use with flexibility and came up with something I find pretty darn helpful. I'm considering this a "beta" for the time being since it's only being used in a few applications and all the different options have not been exhaustively checked., (*2)
Give it a try and let me know what you like, hate, and think needs to be fixed., (*3)
The process for creating a form is simple:, (*4)
Let's walk through these one by one, (*5)
This is pretty simple:, (*6)
$new_form = new PhpFormBuilder();
This uses all the default settings for the form, which are as follows:, (*7)
action: empty, submit to current URL
method: post
enctype: application/x-www-form-urlencoded
class: none
id: none
markup: html
novalidate: false
add_nonce: false
add_honeypot: true
form_element: true
add_submit: true
Explanations for each of the settings are below, (*8)
You can also instantiate by passing in a URL, which becomes the action for the form:, (*9)
$new_form = new PhpFormBuilder('http://submit-here.com');
Once the form has been created, use the set_att
function to change the default attributes:, (*10)
// Add a new form action $new_form->set_att('action', 'http://submit-here.com'); // Change the submit method $new_form->set_att('method', 'get'); // Change the enctype $new_form->set_att('enctype', 'multipart/form-data'); // Can be set to 'html' or 'xhtml' $new_form->set_att('markup', 'xhtml'); // Classes are added as an array $new_form->set_att('class', array()); // Add an id to the form $new_form->set_att('id', 'xhtml'); // Adds the HTML5 "novalidate" attribute $new_form->set_att('novalidate', true); // Adds a WordPress nonce field using the string being passed $new_form->set_att('add_nonce', 'build_a_nonce_using_this'); // Adds a blank, hidden text field for spam control $new_form->set_att('add_honeypot', true); // Wraps the inputs with a form element $new_form->set_att('form_element', true); // If no submit type is added, add one automatically $new_form->set_att('form_element', true);
Currently, there are some restrictions to what can be added but no check as to whether the classes or ids are valid so be mindful of that., (*11)
Inputs can be added one at a time or as a group. Either way, the order they are added is the order in which they'll show up., (*12)
Add fields using their label (in human-readable form), an array of settings, and a name/id slug, if needed., (*13)
$new_form->add_input('I am a little field', array(), 'little_field')
Default and possible settings for field inputs (argument 2):, (*14)
type
, (*15)
name
, (*16)
id
, (*17)
label
, (*18)
value
, (*19)
placeholder
, (*20)
class
, (*21)
options
, (*22)
array('value' => 'Name to show')
min
, (*23)
max
, (*24)
step
, (*25)
autofocus
, (*26)
checked
, (*27)
required
, (*28)
add_label
, (*29)
wrap_tag
, (*30)
wrap_class
, (*31)
wrap_id
, (*32)
wrap_style
, (*33)
One quick statement outputs the form as HTML:, (*34)
$new_form->build_form();
There are a few things that I'd like to correct here and a few features to add. In order of priority:, (*35)