...

View Full Version : Simple form builder/validator.



marek_mar
12-03-2005, 06:12 PM
I've written this simple class as a part of a project I've been doing. It required lots of forms so I quickly saw a need for it.


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)
{
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];
}
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">';
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 . '" 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 . '" id="' . $name . '">' . "\n";
for($j = 2, $m = count($values); $j < $m; $j++)
{
$ret[$i] .= '<option value="' . ($j - 1) . '"' . ((isset($_REQUEST[$name]) && ($_REQUEST[$name] == ($j - 1))) ? ' selected="selected"' : '') . '>' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . '</option>' . "\n";
}
$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++)
{
$ret[$i] .= '<input type="radio" name="' . $name . '" id="' . $name . ($j - 1) . '"' . ' value="' . ($j - 1) . '"' . ((isset($_POST[$name]) && ($_POST[$name] == ($j - 1))) ? ' checked="checked"' : '') . '/><label for="' . $name . ($j - 1) . '">' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . "</lable>\n";
}
break;
case 'checkbox':
$ret[$i] = '<input type="checkbox" name="' . $name . '" id="' . $name . '"' . ' value="1"' . ((isset($_POST[$name]) && $_POST[$name]) ? ' checked="checked"' : '') . '/><label for="' . $name . '">' . (isset($values[1]) ? $values[1] : $values[0]) . "</lable>\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" 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 'submit':
$ret[$i] = '<input type="submit" name="' . $name . '" value="' . $values[1] . '"/>';
break;
default:
$ret[$i] .= '<input type="' . $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) && !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;
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;
}
}

Ok that's the class and now how to use it:


$form = new form('form name', 'file the form should submit to');

Both arguments are required for the constructor.
It returns true.

To add a form element you use the add() method.


$form->add('elenent name', 'element type', 'description', 'default value', /* maximum length */ 10, /* minimum length */ 0, /* size */ 10);

Arguments after "description" are optional. The last three should be either int or float. If the type is "textarea" the $size argument should be a two element array (width, height).
The types are: text, password, textarea, radio, checkbox, select, number*, email and submit.
It returns bool(true) or int(-1) if an element with the same name exists.

To add options to the select or radio element you use the add_option() method.


$form->add_option('name of the select element to which this option belongs', 'the returned value', 'the value presented to the user');

The last argument is optional. The value of the 2nd argument will be used if it is not set.
It returns bool(true) or bool(false) if there is no select element with the given name.

The make() method returns an array with the form. It return an array so you can add whatever you like inbetween the fields.


$form->make(/* show errors */ true);

The argument is optional and defaults to true. It will print errors below the form elemnts that were filled in incorrectly.

The validate() method validates the form.


$form->validate(array('these', 'are', 'the', 'names', 'of elemnts', 'that', 'must be', 'filled in'));

The argument is optional. It must be an array.
It returns bool(true) or an array with all the errors.

The check_submit() method checks if a form has been submited.


if(false !== $form->check_submit())
{
print 'Form was submitted';
}

It returns the name of the submit button user or false.

The get_valid_values() method returns an associative array with all the values that passed the validation.


var_dump($form->get_valid_values());


To add something between the form elements and not have to play with the array returned by form::make() you can add some raw HTML with the raw() method.


$form->raw('Some HTML');


An example:


$form = new form('upload', $_SERVER['PHP_SELF']);
$form->add('filename', 'text', 'Name', false, 100);
$form->add('comment', 'textarea', 'Comments', 'Put comments here', 10, 2, array(100, 20));
$form->add('guess', 'number', 'Guess a numer from 1 to 10', false, 10, 1, 2);
$form->add('order', 'select', 'Sort order');
$form->raw('Hello there!<br>');
$form->add_option('order', 'ASC', 'Ascending');
$form->add_option('order', 'DESC', 'Descending');
$form->add('email', 'email', 'eMail', false, 100);
$form->add('choice', 'radio', 'Are you sure?');
$form->add_option('choice', 'he is stupid', 'No');
$form->add_option('choice', 'he is smart', 'Yes');
$form->add('agreement', 'checkbox', 'I agree to be good');
$form->add('submit', 'submit', 'Upload');
if($form->check_submit())
{
if(true === $form->validate(array('filename', 'agreement')))
{
print 'Form is filled out correctly.';
}
}
print implode("<br />\n", $form->make());
var_dump($form->get_valid_values());


I may update this if needed.

NOTES:
It does not support arrays (element names with []). Then again it might... to some extent... It was not needed.

*The number type is validated by the size of the number not by the length.

Velox Letum
12-03-2005, 08:02 PM
Nice! Very useful. This will definitely come in handy.

marek_mar
12-03-2005, 11:12 PM
This will definitely come in handy.
I can't quite remember, but I think that was a reason why I've written it. :p

Velox Letum
12-04-2005, 03:08 AM
Quite :P

For one, it'll certainly simplify form validation and remove the large blocks of form html from between conditionals...

Rich Pedley
12-05-2005, 10:41 AM
Looks nice, but needs some additional things IMO.

The use of <label> for each form element, which would also require each form element to have a unique id, would enchance this class. Plus it means I may be able to use :-)

Not only that but if the functionality to add a fieldset/legend around a group of form inputs was added it would also be extremely useful.

marek_mar
12-05-2005, 12:27 PM
The class got updated.
There is a new size argument for the add() method.
Checkboxes got added (sort of).
Lables were added to radio and checkbox elemnt types (Do the other need them?).
The return values changed in the check_submit() method.
The example was updated aswell.

Everything which goes around any the form elemnts was left for you to add. That's why the form is returned in an array.

Rich Pedley
12-05-2005, 02:27 PM
Lables were added to radio and checkbox elemnt types (Do the other need them?).
yep fraid so.


Everything which goes around any the form elemnts was left for you to add. That's why the form is returned in an array.
ahh my bad.

marek_mar
12-08-2005, 04:13 PM
Updated it again.
- Everything has a label now.
- A new method was added (form::raw()) to add things between the form elemnts (if you're too lazy to do it with the array that form::make() returns). Anything there will not be validated even if it's a form element. Adding form elements with raw is not suggested as they may break other form elements.

mindlessLemming
12-09-2005, 06:41 AM
That's cool, but it's pretty standard for me to use classes on inputs to make up for IE's lame CSS2 selector support (such as input[type='text'] {....}).
the ones I use every time are:


<input type="text" class="txt" ........ />
<input type="submit" class="btn" ....... />

So how about an optional class attrib? :D

Personally, I'd also move all the add() params to an object literal. That's just preference though, not really a technical consideration ;)

marek_mar
12-09-2005, 04:11 PM
Ok I'll make it add class="elent-type".
And what was that about the params?
I can't esit my first post 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)
{
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];
}
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">';
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++)
{
$ret[$i] .= '<option value="' . ($j - 1) . '"' . ((isset($_REQUEST[$name]) && ($_REQUEST[$name] == ($j - 1))) ? ' selected="selected"' : '') . '>' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . '</option>' . "\n";
}
$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++)
{
$ret[$i] .= '<input type="radio" class="radio" name="' . $name . '" id="' . $name . ($j - 1) . '"' . ' value="' . ($j - 1) . '"' . ((isset($_POST[$name]) && ($_POST[$name] == ($j - 1))) ? ' checked="checked"' : '') . '/><label for="' . $name . ($j - 1) . '">' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . "</lable>\n";
}
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]) . "</lable>\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 '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) && !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;
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;
}
}

marek_mar
12-16-2005, 07:42 PM
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.

samudradaka
01-24-2011, 07:59 AM
Thanks for the very useful form class. One tweak, in the validation section i needed to change:


if(is_array($filled) && in_array($name, $filled) && ($values[0] != 'file' && !strlen($_POST[$name])))

to


if(is_array($filled) && in_array($name, $filled) && ($values[0] != 'file' && $values[0] != 'raw' && !strlen($_POST[$name])))

so adding - && $values[0] != 'raw'

this is in order to prevent 'raw' elements being counted as elements to be validated - thus preventing the form from being validated.

Esteffan
09-19-2011, 04:36 AM
Hello,

does any one of you admins know what's up with user marek mar, initiator of this class?
I tried to pass over a PM to him but his PM storage seems to be overloaded already.
Last activity was about two years ago.

Just wonder if anyone knows something as I would like to contact him due to license questions.

Kind regards,
Stefan

azaiats
09-22-2011, 02:13 AM
Hello,
Just wonder if anyone knows something as I would like to contact him due to license questions.


License? I guess this piece of code can be used without any restrictions otherwise he wouldn't paste it here to help us.

[]s



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum