formName = $formName; } public function fields ($fields) { /** Save the fields into the structure. Available : - name : name of the field in the HTML page - label : label written to the describe the field - [titles] : text written in radio/checkboxes - [defaults] : default values. Must be array for checkbox/select, and string for others - [type] : text, password, hidden, checkbox, select, radio, submit text by default - [multiple] : Multiple selection are possible (if the type supports it) - [group] : define a fieldset and define the title with groupe name Warning : all the elements of the same group must be consecutive ! - [readonly] : put a read-only flag on the field (the user see it but can't interract on it. The value will be sent to next page - [verify] : Tests to verify with error priority and associated message (%s is replaced by field selected value). Order test from main tests to minor tests. \$tmpfield can be used as a copy of the current field, to check the defaults per example - [error] : array containing (error|warning) => message */ $this->fields = $fields; } public function verify (&$fieldsVerify, $valuesVerify = NULL) { /** Return TRUE if the value associated to a field is correct. Return an array with a severity and a message to explain why a field is not correct. Fields can be an array with just one element, then only this element is checked */ $ret = array (); if ($this->debug) echo "
";
foreach ($fieldsVerify as $field)
{
if (!isset ($field->verify))
continue;
if (!isset ($valuesVerify[$field->name]))
throw new Exception ("No value provided for $field->name", 500);
foreach ($field->verify as $test => $message)
{
$func = sprintf ($test, addslashes ($valuesVerify[$field->name]));
if ($this->debug)
echo "VERIFY: \"$func\" => ";
$tmpfield = $field;
$res = addslashes (serialize ($tmpfield));
// TODO : http://fr2.php.net/manual/en/reflectionfunction.invokeargs.php
// to remove eval ?
$rc = eval ("\$tmpfield=unserialize(stripslashes('$res'));".
"return $func;");
if ($this->debug)
var_dump ($rc);
if ($rc !== FALSE)
{
$ret[$field->name] = $message;
$field->error = $message;
break;
}
}
}
if ($this->debug)
{
echo "RESULT: ";
var_dump ($ret);
echo "";
}
return $ret;
}
public function printHTML ($method = 'post', $values = NULL)
{
/** Return the fields in HTML code. If $values is provided, use it in place
of default values. In case of select boxes, $values are the selected
elements
$method is the method written in method field of \n";
return $res;
}
}
class formfield
{
public $name;
public $label;
public $titles;
public $defaults;
public $type;
public $multiple;
public $group;
public $readonly;
public $verify;
public $error;
function __construct ($name, $label)
{
$this->name = $name;
$this->label = $label;
}
}