PDA

View Full Version : Form Builder



whizard
05-26-2007, 07:47 PM
Hello all, I hope this is helpful to someone :)

This is a class I use to build HTML forms.

buildForm.class.php


<?php
class buildForm
{
//Which standard will the form's generated code default to?
var $standard = "xhtml";

//constructor
function buildForm()
{
}

//Did user change standard? update it..
function setStandard($value)
{
$this->standard=$value;
}

//This creates the attribute="value" bits for each input
function checkAttribute($attribute,$value)
{
if(!empty($value))
{
$attr .= " $attribute=\"$value\"";
}
return $attr;
}

function checkOption($option)
{
if(!empty($option))
{
$opt = " $option";
}
return $opt;
}

//Make a text box
function makeField($type,$name,$class='',$id='',$value='',$label='',$size='',$max='',$option='',$data='',$row s='',$cols='')
{
##THESE CAN BE LEFT BLANK
//pre input formatting
$prefix = "<label>$label</label>";
//post input formatting
$suffix = "";

//Make sure required fields are filled
if(empty($type)||empty($name))
{
trigger_error("buildForm error: \$type and/or \$name not specified correctly!",E_USER_ERROR);
}
//Is it a select box?
if($type == "select")
{
//Select Box
//Start Tag
$line = "<select";
//Add Attributes
$line .= $this->checkAttribute('class',$class);
$line .= $this->checkAttribute('id',$id);
$line .= $this->checkAttribute('value',$value);
$line .= $this->checkAttribute('size',$size);
//Allow user to select Multiple?
$line .= $this->checkOption($option);
$line .= ">\n";
//Make sure it has data to fill the options with
if(empty($data))
{
trigger_error("buildForm error: \$data not specified correctly!",E_USER_ERROR);
}
else
{
//is the data in an array?
if(is_array($data))
{
$options = array_merge($data);
}
//or is it in a file?
else
{
include($data);
}
foreach($options as $option)
{
$line .= "<option value=\"$option\">$option</option>\n";
}
}
$line .= "</select>\n";
}
//Is it a textarea?
elseif($type == "textarea")
{
//Textarea
$line = "<textarea";
$line .= $this->checkAttribute('class',$class);
$line .= $this->checkAttribute('id',$id);
$line .= $this->checkAttribute('rows',$rows);
$line .= $this->checkAttribute('cols',$cols);
$line .= $this->checkOption($option);
$line .= ">";
$line .= $this->checkOption($value);
$line .= "</textarea>\n";
}
else
{
$line = "<input";
$line .= " type=\"$type\"";
$line .= " name=\"$name\"";
//Fill in the other attributes, if they're set
$line .= $this->checkAttribute('value',$value);
$line .= $this->checkAttribute('size',$size);
$line .= $this->checkAttribute('maxlength',$max);
$line .= $this->checkAttribute('class',$class);
$line .= $this->checkAttribute('id',$id);
$line .= $this->checkOption($option);
//Close the tag
if($this->standard == "xhtml")
{
$line .= " />\n";
}
elseif($this->standard == "html")
{
$line .= ">\n";
}
else
{
trigger_error("buildForm error: Please specify either 'xhtml' or 'html' for \$standard.",E_USER_ERROR);
}
}
echo $prefix,$line,$suffix;
}
}
?>


example:
some other PHP File:


<?php
include("buildForm.class.php");
$form = new buildForm();
//Change 'xhtml' to 'html' in order to remove slashes from the end of input tags
$form->setstandard('xhtml');
#makeField(type[REQ],name[REQ],class,id,value,label,size,maxlength,option,data (for SELECT elements),rows,cols,)
#'label' sets text for the <label></label> tags.
#'option' is for DISABLED/READONLY/CHECKED/SELECTED options as they may apply to the field. You can specify more than one by just writing your first option and then a space and writing the second
#'data' is either an array or a path to a file with an array named '$options' which holds the data for the select box
//create a text field
$form->makeField('text','test','','','','Text','','','readonly');


//Array of info for select box
//create a select box
$select_box_data = array('1','2','3','4');
$form->makeField('select','select','','','','Select','','','',$select_box_data);
//allow multiple selections
$form->makeField('select','select','','','','Select&nbsp;Multiple','','','multiple',$select_box_data);

//textarea
$form->makeField('textarea','textarea','','','Hi','Textarea','','','','10','20');

//checkboxes
$form->makeField('checkbox','check','','','Checkbox','Checkbox','','checked');
$form->makeField('checkbox','check','','','Checkbox 2','Checkbox 2','','');
?>


This does not currently support Accesskeys, tabindex, or javascript stuff like onclick, etc. However, it would be very easy to add that support for yourself. The main reason I didn't bother with those is that I never really use them and I was trying to cut down on the flood of '', each time the function is called.

The code DOES support XHTML or HTML standards to make it easy for your page to validate.

(<input blah blah blah /> vs <input blah blah blah>)

I'm sure this could be way better and there's lots of room for improvement as well as features I never thought of, so bring on the comments and critiques!

*braces himself*

Anyway, hope this makes someone's day :D
HTH
Dan

schleppel
06-10-2007, 09:20 PM
This does not currently support Accesskeys, tabindex, or javascript stuff like onclick, etc. However, it would be very easy to add that support for yourself. The main reason I didn't bother with those is that I never really use them and I was trying to cut down on the flood of '', each time the function is called.
You could pass optional attributes as 2 element (name, value) arrays. That would allow any attributes without any "'',"s.