HTML Form Creation Class

This form creation class has to be my second most used class after the Database Abstraction Class. It is used to generate XHTML valid forms for projects. Features include ability to handle errors and automatic pre-population of $_POST superglobal as the fields values. I wish to expand this script to allow easier addition of CSS classes to the form elements as the current id and class system is flawed.

Back to Portfolio Page

<?php
/*
*    PHP Form class written by Tam Denholm
*/

class form {

    private 
$submit_button false// Whether a submit button has been added or not
    
private $open_tag '<p>'// Tags surrounding form elements
    
private $close_tag '</p>';
    private 
$post false// Whether to echo POSTDATA as value
    
private $errors = array(); // List of errors
    
private $form_err = array(); // holds errors only to be used form fields
    
private $classes = array(); // string with css classes
    
    // Set surrounding tags
    
public function tags($start$finish){
        
$this->open_tag $start;
        
$this->close_tag $finish;
    }
    
    
// Opening tag plus text
    
private function open($text ''$for ''$class false){
        if(
strlen($for) > && (array_key_exists($for$this->errors) || array_key_exists($for$this->form_err))){
            echo 
str_replace('>'' class="form_error">'$this->open_tag)."\n";
        }else{
            echo 
$this->open_tag."\n";
        }
        
        
$this->set_classes(array($for$class));
        if(
strlen($text) > 0){
            echo 
'<label ';
            if(
strlen($for) > 0) echo 'for="f_'.$for.'" ';
            echo 
'class="'.$this->get_classes().'">'.$text."</label>\n";
        }
    }
    
    
// Closing tag
    
private function close(){
        echo 
$this->close_tag."\n";
    }
    
    
// Add errors
    // $errors[$name] = $error;
    
public function errors($arr = array(), $all false){
        if(
count($arr) > 0){
            
$this->errors $arr// assigns errors
        
}else{
            return;
        }
        
// outputs errors with no tag
        
if(count($this->errors) > 0){
            foreach(
$this->errors as $key => $value){
                if(
strlen($value) == 0) continue;
                if(!
is_string($key) && $all === false){ 
                    echo 
'<p class="error">'.$value.'</p>'."\n";
                }elseif(
$all !== false){ // if display $all errors
                    
echo '<p class="error">'.$value.'</p>'."\n";
                    unset(
$this->errors[$key]); // delete so its not echoed after field
                    
$this->form_err[$key] = $value;
                }
            }
        }
    }
    
    
// css set class handler method
    
private function set_classes($input false){
        if(
is_array($input)){
            foreach(
$input as $value){
                
$this->classes[] = $value;
            }
        }elseif(
is_string($input)){
            
$this->classes[] = $input;
        }
    }
    
    
// css get class handler method
    
private function get_classes(){
        if(
count($this->classes) > 0){
            
$this->classes array_unique($this->classes);
            foreach(
$this->classes as $value){
                
$classes .= $value.' ';
            }
            unset(
$this->classes);
            return 
trim($classes);
        }
    }

    
// Opening form
    
public function start_form($action false$method 'post'$file false){
        echo 
"\n".'<!-- Start Automatic Form Generation -->'."\n";
        if(!
$action){
            
$action addslashes($_SERVER['REQUEST_URI']);
        }
        if(
$method != 'get' && $method != 'post'){
            return 
false;
        }
        echo 
'<form action="'.$action.'" method="'.$method.'"';
        if(
$file){
            echo 
' enctype="multipart/form-data" ';
        }
        echo 
'>'."\n";
    }
    
    
// Ending of the form, auto adds submit button if not added
    
public function end_form($text false){
        if(!
$this->submit_button){
            if(!
$text$text 'Submit';
            
$this->submit($text);
        }
        echo 
"</form>\n";
        echo 
'<!-- End Automatic Form Generation -->'."\n\n";
    }
    
    
// Input, no type
    
private function input($type$name$value ''$attr = array()){
        if(!
$value && $_POST[$name] && $this->post && $type != 'password' && $type != 'radio'){
            
$value $_POST[$name];
        }
        
$this->set_classes(array($type));
        
// Extra attributes
        
if(count($attr) > 0){
            foreach(
$attr as $attr_name => $attr_value){
                if(
$attr_name == 'class'){
                    
$this->set_classes($attr_value);
                }else{
                    
$attributes .= ' '.$attr_name.'="'.$attr_value.'"';
                }
            }
        }
        
        if(
array_key_exists($name$this->errors) || array_key_exists($name$this->form_err)){
            
$this->set_classes('error');
        }
        if(
array_key_exists($name$this->errors)){
            if(
$type != 'radio' && $type != 'checkbox'){
                
$error '<p class="error">'.$this->errors[$name].'</p>';
            }
        }
        
        echo 
'<input type="'.$type.'" name="'.$name.'" ';
        if(
$type != 'radio') echo 'id="f_'.$name.'" ';
        echo 
'value="'.$value.'" class="'.$this->get_classes().'"'.$attributes.' />'."\n";
        echo 
$error."\n";
    }
    
    
// Simple text input
    
public function text($text$name$value ''$attr = array()){
        
$this->open($text$name);
        
$this->input('text'$name$value$attr);
        
$this->close();
    }
    
    
// Hidden input
    
public function hidden($name$value ''){
        
$this->input('hidden'$name$value);
    }
    
    
// Password input
    
public function pass($text$name){
        
$this->open($text$name);
        
$this->input('password'$name);
        
$this->close();
    }
    
    
// File input
    
public function file($text$name){
        
$this->open($text$name);
        
$this->input('file'$name);
        
$this->close();
    }
    
    
// Textarea
    
public function textarea($text$name$value false$cols 30$rows 8){
        if(!
$value && $_POST[$name] && $this->post){
            
$value $_POST[$name];
        }
        
$this->open($text$name);
        echo 
'<textarea id="f_'.$name.'" name="'.$name.'" cols="'.$cols.'" rows="'.$rows.'">'.$value.'</textarea>';
        
// error
        
if(array_key_exists($name$this->errors)){
            echo 
'<p class="error">'.$this->errors[$name].'</p>';
        }
        
$this->close();
    }
    
    
// Select
    
public function select($text$name$options = array(), $selected false){
        
$this->open($text$name);
        echo 
'<select name="'.$name.'" id="'.$name.'">'."\n";
        if(
count($options) > 0){
            
$first true;
            foreach(
$options as $key => $value){
                if(
$key === && $first === true){ // perhaps too presumptuous
                    
$first false;
                    
$cheat true;
                }
                if(
$cheat === true){
                    echo 
'<option value="'.$value.'"';                
                }else{
                    echo 
'<option value="'.$key.'"';
                }
                
// hack to change numeric string to int so type comparision works
                
if(is_numeric($selected)) $selected intval($selected);
                if(
is_numeric($key)) $key intval($key);
                if(
is_numeric($_POST[$name])) $_POST[$name] = intval($_POST[$name]);
                if(
$_POST[$name] === $key && $this->post){
                    echo 
' selected="selected" ';
                }elseif(
$selected === $key){ // must be type so 0 != false
                    
echo ' selected="selected" ';
                }
                echo 
'>'.$value.'</option>'."\n";
            }
        }
        echo 
'</select>'."\n";
        
// error
        
if(array_key_exists($name$this->errors)){
            echo 
'<p class="error">'.$this->errors[$name].'</p>';
        }
        
$this->close();
    }
    
    
// Radio buttons
    
public function radio($text$name$buttons = array(), $checked false){
        
$this->open($text);
        if(
count($buttons) > 0){
            echo 
'<ul class="radio_list">'."\n";
            foreach(
$buttons as $value => $opt_text){
                if((
$checked == $value && !$_POST) || ($_POST && $this->post && $_POST[$name] == $value)){
                    
$attr = array('checked' => 'checked');
                }
                echo 
'<li>';
                
$this->input('radio'$name$value$attr);
                
$attr = array();
                echo 
$opt_text."</li>\n";
            }
            
// error
            
if(array_key_exists($name$this->errors)){
                echo 
'<li class="error">'.$this->errors[$name].'</li>';
            }
            echo 
"</ul>\n";
        }
        
$this->close();
    }
    
    
// Checkbox
    
public function checkbox($text$name$checked false){
        if((
$_POST[$name] == '1' && $this->post) || $checked){
            
$attr = array('checked' => 'checked');
        }
        
        
$this->open($text$name'checkbox');
        
$this->input('checkbox'$name'1'$attr);
        
$this->close();
        
// error
        
if(array_key_exists($name$this->errors)){
            echo 
'<p class="error">'.$this->errors[$name].'</p>';
        }
    }
    
    
// Submit button
    
public function submit($value 'Submit'$name false){
        
$this->submit_button true;
        
$this->open(''$name);
        echo 
'<input type="submit" ';
        if(
$name){
            echo 
'name="'.$name.'" ';
        }
        echo 
'id="f_'.strtolower($name).'" class="submit" value="'.$value.'" />'."\n";
        
$this->close();
    }
    
    
// Image submit button
    
public function image_submit($src$name false$attr = array()){
        
$this->submit_button true;
        
$this->open(''$name);
        echo 
'<input type="image" ';
        if(
$name){
            echo 
'name="'.$name.'" ';
        }
        
// Extra attributes
        
if(count($attr) > 0){
            foreach(
$attr as $attr_name => $attr_value){
                echo 
$attr_name.'="'.$attr_value.'"'.' ';
            }
        }
        echo 
'src="'.$src.'" alt="Submit" class="image" />'."\n";
        
$this->close();
    }
    
    
// Sets echoing of POSTDATA as value
    // Has ability to turn on and off
    
public function post(){
        if(
$this->post === true){
            
$this->post false;
        }elseif(
$this->post === false){
            
$this->post true;
        }
    }
    
    
// load an array as default values
    
public function defaults($arr = array()){
        
// may possibly cause errors overwriting the $_POST array
        
if(count($arr) == 0) return false;
        foreach(
$arr as $key => $value){
            if(isset(
$_POST[$key])){
                continue;
            }
            
$_POST[$key] = $value;
        }
    }

}

?>