My Form Builder Class
This class is designed to generate an HTML form using the following elements:
- Textboxes (with and without labels)
- Password Boxes (with and without labels)
- Check Boxes
- Radio Buttons
The class is set up to allow for method chaining and can be used as in the following example:
require 'class.form.php'; $form = new FormBuilder(); $form->beginForm('testForm', 'form.php') ->createTextBoxWithLabel('Name: ', 'theName') ->endForm() ->echoOut();
Script
/** * This is for the Form Builder class * * PHP Version 5.2 * * @category Base_Classes * @package FormBuilder * @author Joseph Thayne* @license http://www.google.com Unknown License * @link http://www.mythoughtexactly.com */ /** * FormBuilder class * * @category Base_Classes * @package FormBuilder * @author Joseph Thayne * @copyright 2011 Joseph Thayne * @license http://www.google.com Unknown License * @version Release: 2.0 * @link http://www.mythoughtexactly.com */ class FormBuilder { private $form; /** * __construct * * @access protected * @return void */ function __construct() { } /** * _defineAttribute * * @param mixed $name The name of the attribute * @param mixed $value The value of the attribute * * @access private * @return string */ private function _defineAttribute($name, $value) { if ($value === null) { $output = ""; } else { $output = " $name='$value'"; } return $output; } /** * beginForm Open a form with the appropriate attributes * * @param string $name The name of the form * @param string $action The url the form will be submitted to * @param string $method The method the form will be submitted. * Options are GET and POST * @param array $attributes Array holding potential attributes for the form * element. Possible values are id, class, * enctype, and style * * @access public * @return string */ function beginForm($name, $action, $method="POST", $attributes = array()) { /** * Possible attributes for $attributes are: * id, class, enctype, and style */ $attArray = array(); foreach ($attributes as $name=>$value) { $attArray[] = $this->_defineAttribute($name, $value); } $att = join(' ', $attArray); $output = "".$followingText.PHP_EOL; return $this; } /** * createLabel Creates a label tag for an associated form element * * @param mixed $value The text of the label * @param mixed $name The name of the element the label refers to * @param mixed $prefix Text that will display just before the label tag * @param mixed $suffix Text that will display just after the label tag * * @access public * @return string */ function createLabel($value, $name, $prefix=null, $suffix=null) { $this->form .= $prefix."".$suffix.PHP_EOL; return $this; } /** * createTextBox Creates a text box form element * * @param string $name The name of the text box * @param array $attributes Array holding potential attributes for the * form element. Possible values are value, id, * class, size, maxlength, and style * * @access public * @return string */ function createTextBox($name, $attributes = array()) { /** * Possible attributes for $attributes are: * value, id, class, size, maxlength, and style */ $attArray = array(); foreach ($attributes as $name=>$value) { $attArray[] = $this->_defineAttribute($name, $value); } $att = join(' ', $attArray); $output = "".PHP_EOL; $this->form .= $output; return $this; } /** * createTextBoxWithLabel Creates a text box form element with associated label tag * * @param string $label The text of the label * @param string $name The name of the text box * @param array $attributes Array holding potential attributes for the * element. Possible values are value, id, * class, size, maxlength, and style * @param string $labelPrefix Text that will display just before the * label tag * @param string $labelSuffix Text that will display just after the * label tag * * @access public * @return string */ function createTextBoxWithLabel($label, $name, $attributes = array(), $labelPrefix=null, $labelSuffix=null) { /** * Possible attributes for $attributes are: * value, id, class, size, maxlength, and style */ $this->createLabel($label, $name, $labelPrefix, $labelSuffix) ->createTextBox($name, $attributes); return $this; } /** * createPasswordBox Creates a password box form element * * @param string $name The name of the passwordbox * @param array $attributes Array holding potential * attributes for the element. * Possible values are id, class, * size, maxlength, and style * @param boolean $showPasswordWhileTyping Display the password while * typing instead of masking it * * @access public * @return string */ function createPasswordBox($name, $attributes, $showPasswordWhileTyping=false) { /** * Possible attributes for $attributes are: * id, class, size, maxlength, and style */ $showPasswordWhileTyping = (bool)$showPasswordWhileTyping; if ($showPasswordWhileTyping === true) { $this->createTextBox($name, "", $attributes); } else { $attArray = array(); foreach ($attributes as $name=>$value) { $attArray[] = $this->_defineAttribute($name, $value); } $att = join(' ', $attArray); $output = "".PHP_EOL; $this->form .= $output; } return $this; } /** * createPasswordBoxWithLabel Creates a password box form element with associated label tag * * @param string $label The text of the label * @param string $name The name of the passwordbox * @param array $attributes Array holding potential * attributes for the element. * Possible values are id, class, * size, maxlength, and style * @param int $showPasswordWhileTyping Display the password while typing * instead of masking it * @param string $labelPrefix Text that will display just * before the label tag * @param string $labelSuffix Text that will display just * after the label tag * * @access public * @return string */ function createPasswordBoxWithLabel($label, $name, $attributes=array(), $showPasswordWhileTyping=0, $labelPrefix=null, $labelSuffix=null) { $this->createLabel($label, $name, $labelPrefix, $labelSuffix) ->createPasswordBox($name, $attributes, $showPasswordWhileTyping); return $this; } /** * createCheckBox Creates a checkbox form element * * @param string $name The name of the check box * @param string $value The value of the field * @param boolean $checked Is the check box checked by default * @param array $attributes Array holding potential attributes for the * element. Possible values are id, class, * and style * * @access public * @return string */ function createCheckBox($name, $value, $textPreceeding='', $textFollowing='', $checked=false, $attributes=array()) { $attArray = array(); foreach ($attributes as $name=>$value) { $attArray[] = $this->_defineAttribute($name, $value); } $att = join(' ', $attArray); $ckText = ''; if ($checked === true) { $ckText = " checked='checked'"; } $output = "$textPreceeding$textFollowing"; $this->form .= $output; return $this; } /** * createRadioButton Creates a radio button form element * * @param string $name The name of the radio button * @param string $value The value of the field * @param string $text The text displaying at the end of the field * @param boolean $checked Is the radio button selected by default * @param array $attributes Array holding potential attributes for the * element. Possible values are id, class, * and style * * @access public * @return string */ function createRadioButton($name, $value, $textPreceeding='', $textFollowing='', $checked=false, $attributes=array()) { $attArray = array(); foreach ($attributes as $name=>$value) { $attArray[] = $this->_defineAttribute($name, $value); } $att = join(' ', $attArray); $ckText = ''; if ($checked === true) { $ckText = " checked='checked'"; } $output = "$textPreceeding$textFollowing"; $this->form .= $output; return $this; } /** * echoOut Print the generated form and clear the register. * * @access public * @return void */ function echoOut() { echo $this->form; $this->form = ''; } }