Ok I did a big update to this class (mostly bugfixes though).
- Label tags are SPELLED correctly this time.
- Fixed the way checkbox values are returned by form::get_valid_values().
- You can now select checked/selected by default radio/option.
- File field has been added ($max argument checks for the filesize).
- Probably some other details I don'tremember anymore.
class form
{
var $name = '';
var $action = '';
var $data = array();
var $error = array();
var $valid = array();
var $submit = array();
function form($name, $action)
{
$this->name = $name;
$this->action = $action;
return true;
}
function add($name, $type, $human_value, $value = false, $max = false, $min = false, $size = false)
{
if(isset($this->data[$name]))
{
return -1;
}
if($type == 'submit')
{
$this->submit[] = $name;
}
if(!$type != 'select')
{
$this->data[$name] = array_slice(func_get_args(), 1);
return true;
}
$this->data[$name] = array(func_get_arg(1), array_slice(func_get_args(), 1));
return true;
}
function add_option($select, $value, $human_value = false, $default = false)
{
if(!isset($this->data[$select]) || ($this->data[$select][0] != 'select' && $this->data[$select][0] != 'radio'))
{
return false;
}
$this->data[$select][] = array_slice(func_get_args(), 1);
return true;
}
function raw($data)
{
$this->data[] = array('raw', $data);
return true;
}
function check_submit()
{
for($i = 0, $n = count($this->submit); $i < $n; $i++)
{
if(isset($_POST[$this->submit[$i]]))
{
return $this->submit[$i];
break;
}
}
return false;
}
function get_valid_values()
{
$ret = array();
for($i = 0, $n = count($this->valid); $i < $n ; $i++)
{
if($this->data[$this->valid[$i]][0] == 'select' || $this->data[$this->valid[$i]][0] == 'radio')
{
$ret[$this->valid[$i]] = $this->data[$this->valid[$i]][$_POST[$this->valid[$i]] + 1][0];
}
elseif($this->data[$this->valid[$i]][0] == 'file')
{
if(isset($_FILES[$name]) && $_FILES[$name]['error'] == 4)
{
continue;
}
$ret[$this->valid[$i]] = $_FILES[$this->valid[$i]];
}
elseif($this->data[$this->valid[$i]][0] == 'checkbox')
{
if($_POST[$this->valid[$i]] === '1')
{
$ret[$this->valid[$i]] = $_POST[$this->valid[$i]];
}
}
else
{
$ret[$this->valid[$i]] = $_POST[$this->valid[$i]];
}
}
return $ret;
}
function make($show_errors = true)
{
$ret = array();
$submit = $this->check_submit();
$i = 1;
$ret[] = '<form name="' . $this->name . '" action="' . $this->action . '" method="POST" enctype="multipart/form-data">';
foreach($this->data as $name => $values)
{
$ret[$i] = (strlen($values[1]) && $values[0] != 'raw') ? '<label for="' . $name . '">' . $values[1] . ': </label>' : '';
switch($values[0])
{
case 'raw':
$ret[$i] = $values[1];
break;
case 'textarea':
$ret[$i] .= '<br /><textarea name="' . $name . '" class="textarea" id="' . $name . '"' . ((isset($values[5]) && is_array($values[5])) ? 'cols="' . $values[5][0] . '" rows="' . $values[5][1] . '"' : '') . '>' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '</textarea>';
$ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" : '';
break;
case 'select':
$ret[$i] .= '<select name="' . $name . '" class="select" id="' . $name . '">' . "\n";
for($j = 2, $m = count($values); $j < $m; $j++)
{
$selected = false;
$select = '';
if(isset($_REQUEST[$name]) && $_REQUEST[$name] == ($j - 1))
{
$select = ' selected="selected"';
$selected = true;
}
if(!$selected && $values[$j][2])
{
$select = ' selected="selected"';
}
$ret[$i] .= '<option value="' . ($j - 1) . '"' . $select . '>' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . '</option>' . "\n";
}
$select = '';
$selected = false;
$ret[$i] .= '</select>';
$ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" : '';
break;
case 'radio':
for($j = 2, $m = count($values); $j < $m; $j++)
{
$selected = false;
$select = '';
if(isset($_REQUEST[$name]) && $_REQUEST[$name] == ($j - 1))
{
$select = ' checked="checked"';
$selected = true;
}
if(!$selected && $values[$j][2])
{
$select = ' checked="checked"';
}
$ret[$i] .= '<input type="radio" class="radio" name="' . $name . '" id="' . $name . ($j - 1) . '"' . ' value="' . ($j - 1) . '"' . $select . '/><label for="' . $name . ($j - 1) . '">' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . "</label>\n";
}
$select = '';
$selected = false;
break;
case 'checkbox':
$ret[$i] = '<input type="checkbox" class="checkbox" name="' . $name . '" id="' . $name . '"' . ' value="1"' . ((isset($_POST[$name]) && $_POST[$name]) ? ' checked="checked"' : '') . '/><label for="' . $name . '">' . (isset($values[1]) ? $values[1] : $values[0]) . "</label>\n";
$ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" : '';
break;
case 'number':
case 'email':
$ret[$i] .= '<input type="text" class="text" name="' . $name . '" id="' . $name . '"' . (((isset($values[2]) && $values[2]) || $submit) ? ' value="' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '"' : '') . ((isset($values[5]) && $values[5]) ? ' size="' . $values[5] . '"' : '') . ((isset($values[3]) && $values[3]) ? ' maxlength="' . (($values[0] == 'number') ? strlen($values[3]) : $values[3]) . '"' : '') . '/>';
$ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" : '';
break;
case 'file':
$ret[$i] .= '<input type="' . $values[0] . '" class="' . $values[0] . '" name="' . $name . '" id="' . $name . '"' . (((isset($values[2]) && $values[2]) || $submit) ? ' value="' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '"' : '') . ((isset($values[5]) && $values[5]) ? ' size="' . $values[5] . '"' : '') . ((isset($values[3]) && $values[3]) ? ' maxlength="' . $values[3] . '"' : '') . '/>';
$ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" : '';
break;
case 'submit':
$ret[$i] = '<input type="submit" class="text" name="' . $name . '" value="' . $values[1] . '"/>';
break;
default:
$ret[$i] .= '<input type="' . $values[0] . '" class="' . $values[0] . '" name="' . $name . '" id="' . $name . '"' . (((isset($values[2]) && $values[2]) || $submit) ? ' value="' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '"' : '') . ((isset($values[5]) && $values[5]) ? ' size="' . $values[5] . '"' : '') . ((isset($values[3]) && $values[3]) ? ' maxlength="' . $values[3] . '"' : '') . '/>';
$ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" : '';
break;
}
$i++;
$ret[] = '</form>';
}
return $ret;
}
function validate($filled = false)
{
foreach($this->data as $name => $values)
{
if(!isset($_POST[$name]))
{
$_POST[$name] = '';
}
$_POST[$name] = trim($_POST[$name]);
$_POST[$name] = (get_magic_quotes_gpc()) ? stripslashes($_POST[$name]) : $_POST[$name];
if(is_array($filled) && in_array($name, $filled) && ($values[0] != 'file' && !strlen($_POST[$name])))
{
$this->error[$name] = 'Value must be set.';
continue;
}
switch($values[0])
{
case 'raw':
break;
case 'number':
if(!is_numeric($_POST[$name]))
{
$this->error[$name] = 'Value must be a number.';
}
if((isset($values[3]) && $values[3] !== false && (float) $_POST[$name] > (float) $values[3]) || (isset($values[4]) && $values[4] !== false && (float) $_POST[$name] < (float) $values[4]))
{
$this->error[$name] = 'Value is too big/small. Value should be between ' . ((isset($values[4])) ? $values[4] : '0') . ' and ' . $values[3] . '.';
}
break;
case 'select':
case 'radio':
if(!is_numeric($_POST[$name]))
{
$this->error[$name] = 'Unpredicted value.';
}
break;
case 'email':
if(!preg_match('/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $_POST[$name]))
{
$this->error[$name] = 'Value is not an email address.';
}
break;
case 'file':
if(in_array($name, $filled) && isset($_FILES[$name]) && $_FILES[$name]['error'] == 4)
{
$this->error[$name] = 'Value must be set.';
break;
}
if((isset($values[3]) && $values[3] !== false && $_FILES[$name]['size'] >= $values[3]))
{
$this->error[$name] = 'File is too large. The size limit is ' . ($values[3] / 1024) . 'kB.';
}
break;
default:
if((isset($values[3]) && $values[3] !== false && strlen($_POST[$name]) >= $values[3]) || (isset($values[4]) && $values[4] !== false && strlen($_POST[$name]) <= $values[4]))
{
$this->error[$name] = 'Value is too long/short. Value should be between ' . ((isset($values[4])) ? $values[4] : '0') . ' and ' . $values[3] . ' characters long.';
}
break;
}
if(!isset($this->error[$name]))
{
$this->valid[] = $name;
}
}
if(count($this->error))
{
return $this->error;
}
return true;
}
}
Have fun building your forms with it.