setFormMethod('REQUEST');
//$form->setDatabaseTable(DB_T_PREFIX . 'example_table');
//$form->setRequiredMarkPosition(FORM_REQ_MARK_POS_RIGHT);
//$form->setRequiredMark('HTML: *');
//$form->setQuickPrintLabelSuffix('');
$fldName =& new formFieldText($form, 'Name');
$fldName->setMinLength('Your name is required.', 1);
$fldName->setMaxLength('Your name cannot be longer than XXX characters.', 250);
//$fldName->setDatabaseField('name');
//$fldName->setFieldClass('myClass');
//$fldName->setLabelClass('myClass');
//$fldName->quickPrintShow(false);
$fldEmail =& new formFieldEmail($form, 'Email');
$fldEmail->setFormatError('Your email does not appear to be correct.');
$fldEmail->setMinLength('Your email is required.', 1);
$fldEmail->setMaxLength('Your email cannot be longer than XXX characters.', 250);
//--------------------------------------------------
// If the user has submitted the form
if (isset($_POST['act']) && $_POST['act'] != '') {
//--------------------------------------------------
// Additional validation
//$fldName->setError('Set the error - e.g. This username is already in use');
//$form->addError('General form error - e.g. System error, please try again later');
//--------------------------------------------------
// If the data is valid
if (!$form->hasError()) {
//--------------------------------------------------
// Process
//$fldName->getValue();
//$fldEmail->getValue();
//--------------------------------------------------
// Send the user onto the thank-you page
goto('./thankYou/');
}
//--------------------------------------------------
// Return the error list
$htmlErrorList = $form->getHtmlErrorList();
}
//--------------------------------------------------
// Create the form
?>
//--------------------------------------------------
// End of example setup
***************************************************/
//--------------------------------------------------
// Script configuration
$GLOBALS['tplErrorListId'] = 'errorList';
//--------------------------------------------------
// Easy to use constants
define('FORM_REQ_MARK_POS_LEFT', 0);
define('FORM_REQ_MARK_POS_RIGHT', 1);
define('FORM_REQ_MARK_POS_NONE', 2);
define('FORM_DATABASE_FIELD_VALUE', 0);
define('FORM_DATABASE_FIELD_KEY', 1);
//--------------------------------------------------
// Master form class
class form {
var $fields;
var $fieldCount;
var $formMethod;
var $htmlErrors;
var $htmlRequiredMark;
var $requiredMarkPosition;
var $hasRunPostProcessValidation;
var $databaseTable;
var $databaseRecordSelect;
var $databaseFieldDetails;
var $databaseFieldValues;
var $quickPrintLabelSuffix;
function form() {
$this->fields = array();
$this->fieldCount = 0;
$this->formMethod = 'POST';
$this->htmlErrors = array();
$this->htmlRequiredMark = NULL;
$this->requiredMarkPosition = FORM_REQ_MARK_POS_LEFT;
$this->hasRunPostProcessValidation = false;
$this->databaseTable = NULL;
$this->databaseRecordSelect = NULL;
$this->databaseFieldDetails = array();
$this->databaseFieldValues = array();
$this->quickPrintLabelSuffix = ':';
}
function setFormMethod($method) {
$this->formMethod = $method;
}
function getFormMethod() {
return $this->formMethod;
}
function _addField(&$fieldObj) {
$fieldId = $this->fieldCount++;
$this->fields[$fieldId] =& $fieldObj;
return $fieldId;
}
function getFields() {
return $this->fields;
}
function getField($id) {
return $this->fields[$id];
}
function _htmlEncode($string) {
if (substr($string, 0, 5) == 'HTML:') {
if (substr($string, 5, 1) == ' ') { // Optional separating space
return substr($string, 6);
} else {
return substr($string, 5);
}
} else {
return html($string);
}
}
function setRequiredMark($requiredMark) {
$this->htmlRequiredMark = $this->_htmlEncode($requiredMark);
}
function setRequiredMarkPosition($position) {
if ($position == FORM_REQ_MARK_POS_LEFT || $position == FORM_REQ_MARK_POS_RIGHT || $position == FORM_REQ_MARK_POS_NONE) {
$this->requiredMarkPosition = $position;
} else {
exit('
Invalid required mark position specified - use FORM_REQ_MARK_POS_* constants
');
}
}
function getRequiredMark($requiredMarkPosition = FORM_REQ_MARK_POS_LEFT) {
if ($this->htmlRequiredMark !== NULL) {
return $this->htmlRequiredMark;
} else if ($requiredMarkPosition == FORM_REQ_MARK_POS_RIGHT) {
return ' *';
} else {
return '* ';
}
}
function getRequiredMarkPosition() {
return $this->requiredMarkPosition;
}
function setQuickPrintLabelSuffix($suffix) {
$this->quickPrintLabelSuffix = $suffix;
}
function _runPostProcessValidation() {
if ($this->hasRunPostProcessValidation) {
return NULL;
}
for ($fieldId = 0; $fieldId < $this->fieldCount; $fieldId++) {
$this->fields[$fieldId]->_postProcessValidation();
}
$this->hasRunPostProcessValidation = true;
}
function getHtmlErrors() {
$this->_runPostProcessValidation();
$htmlErrorsFlat = array();
for ($fieldId = -1; $fieldId < $this->fieldCount; $fieldId++) { // In field order, starting with -1 for general form errors
if (isset($this->htmlErrors[$fieldId])) {
foreach ($this->htmlErrors[$fieldId] as $htmlError) {
$htmlErrorsFlat[] = $htmlError;
}
}
}
return $htmlErrorsFlat;
}
function getHtmlErrorList() {
$htmlErrorsFlat = $this->getHtmlErrors();
$htmlOutput = '';
if (count($htmlErrorsFlat) > 0) {
$htmlOutput = '
';
foreach ($htmlErrorsFlat as $err) $htmlOutput .= '
' . $err . '
';
$htmlOutput .= '
';
}
return $htmlOutput;
}
function _addFieldError($fieldId, $error, $hiddenInfo = NULL) {
if (function_exists('formErrorOverride')) {
if ($fieldId == -1) {
$error = formErrorOverride($error, $this, NULL);
} else {
$error = formErrorOverride($error, $this, $this->getField($fieldId));
}
}
if (!isset($this->htmlErrors[$fieldId])) {
$this->htmlErrors[$fieldId] = array();
}
$htmlError = $this->_htmlEncode($error);
if ($hiddenInfo !== NULL) {
$htmlError .= ' ';
}
$this->htmlErrors[$fieldId][] = $htmlError;
}
function _setFieldError($fieldId, $error, $hiddenInfo = NULL) {
$this->htmlErrors[$fieldId] = array();
$this->_addFieldError($fieldId, $error, $hiddenInfo);
}
function addError($error) {
$this->_addFieldError(-1, $error); // -1 is for general errors, not really linked to a field
}
function addHtmlError($htmlError) { // Backwards compatibility
$this->addError('HTML:' . $htmlError);
}
function _fieldHasError($fieldId) {
return ($this->hasRunPostProcessValidation && isset($this->htmlErrors[$fieldId]));
}
function hasError() {
$this->_runPostProcessValidation();
return (count($this->htmlErrors) > 0);
}
function setDatabaseTable($sqlTable, $db = NULL) {
//--------------------------------------------------
// Store
$this->databaseTable = $sqlTable;
//--------------------------------------------------
// Field details
$this->databaseFields = array();
if ($db == NULL) {
$db = $GLOBALS['db'];
}
$rst = $db->query('SELECT * FROM ' . $sqlTable . ' LIMIT 0', false); // Don't return ANY data, and don't run debug (can't have it asking for "deleted" columns).
for ($k = (mysql_num_fields($rst) - 1); $k >= 0; $k--) {
$mysqlField = mysql_fetch_field($rst, $k);
if (strpos(mysql_field_flags($rst, $k), 'enum') !== false) {
$type = 'enum';
$values = $db->enumValues($sqlTable, $mysqlField->name);
} else {
$type = $mysqlField->type;
$values = NULL;
}
$length = mysql_field_len($rst, $k); // $mysqlField->max_length returns 0
if ($GLOBALS['pageCharset'] == 'UTF-8' && ($type == 'blob' || $type == 'string')) {
$length = ($length / 3);
}
$this->databaseFields[$mysqlField->name]['length'] = $length;
$this->databaseFields[$mysqlField->name]['type'] = $type;
$this->databaseFields[$mysqlField->name]['values'] = $values;
}
}
function getDatabaseTable() {
return $this->databaseTable;
}
function getDatabaseTableFields() {
return $this->databaseFields;
}
function setDatabaseRecordSelect($sqlWhere) {
$this->databaseRecordSelect = $sqlWhere;
}
function getDatabaseRecordValues($db = NULL) {
//--------------------------------------------------
// Validation
if ($this->databaseTable === NULL) exit('
You need to call "setDatabaseTable", on the form object
');
if ($this->databaseRecordSelect === NULL) exit('
You need to call "setDatabaseRecordSelect", on the form object
');
//--------------------------------------------------
// Fields
$fields = array();
for ($fieldId = 0; $fieldId < $this->fieldCount; $fieldId++) {
if ($this->fields[$fieldId]->databaseFieldName !== NULL) {
$fields[$fieldId] = $this->fields[$fieldId]->databaseFieldName;
}
}
//--------------------------------------------------
// Get
if ($db == NULL) {
$db = $GLOBALS['db'];
}
$db->select($this->databaseTable, $fields, $this->databaseRecordSelect);
if ($row = $db->fetchAssoc()) {
foreach ($row as $cField => $cValue) {
$key = array_search($cField, $fields);
if ($key !== false) {
if ($this->fields[$key]->databaseFieldKey == FORM_DATABASE_FIELD_KEY) {
$this->fields[$key]->setValueKey($cValue);
} else {
$this->fields[$key]->setValue($cValue);
}
}
}
}
}
function setDatabaseFieldValue($name, $value) {
$this->databaseFieldValues[$name] = $value;
}
function databaseSave($db = NULL) {
//--------------------------------------------------
// Validation
if ($this->databaseTable === NULL) exit('
You need to call "setDatabaseTable", on the form object
');
//--------------------------------------------------
// Values
$values = array();
for ($fieldId = 0; $fieldId < $this->fieldCount; $fieldId++) {
if ($this->fields[$fieldId]->databaseFieldName !== NULL) {
$fieldName = $this->fields[$fieldId]->databaseFieldName;
$fieldKey = $this->fields[$fieldId]->databaseFieldKey;
$fieldType = $this->databaseFields[$fieldName]['type'];
if ($fieldType == 'datetime' || $fieldType == 'date') {
$values[$fieldName] = $this->fields[$fieldId]->getValueDate();
} else if ($fieldKey == FORM_DATABASE_FIELD_KEY) {
$values[$fieldName] = $this->fields[$fieldId]->getValueKey();
} else {
$values[$fieldName] = $this->fields[$fieldId]->getValue();
}
}
}
foreach ($this->databaseFieldValues as $cName => $cValue) { // More reliable array_merge at keeping keys
$values[$cName] = $cValue;
}
if (isset($this->databaseFields['edited'])) {
$values['edited'] = date('Y-m-d H:i:s');
}
if (isset($this->databaseFields['created']) && $this->databaseRecordSelect === NULL) {
$values['created'] = date('Y-m-d H:i:s');
}
//--------------------------------------------------
// Save
if ($db == NULL) {
$db = $GLOBALS['db'];
}
if ($this->databaseRecordSelect === NULL) {
$db->insert($this->databaseTable, $values);
} else {
$db->update($this->databaseTable, $values, $this->databaseRecordSelect);
}
}
function htmlQuickPrint($fields = NULL) {
//--------------------------------------------------
// Print all fields, if not all provided
if ($fields === NULL) {
$fields =& $this->fields;
}
//--------------------------------------------------
// Create the output
$htmlOutput = '';
foreach (array_keys($fields) as $fieldId) {
if ($fields[$fieldId]->quickPrintShow()) {
$htmlOutput .= $fields[$fieldId]->htmlQuickPrint();
}
}
return $htmlOutput;
}
}
//--------------------------------------------------
// Basic form field functionality
class formFieldBase {
var $formObj;
var $name;
var $htmlLabel;
var $htmlRequiredMark;
var $requiredMarkPosition;
var $required;
var $id;
var $cssClassField;
var $cssClassLabel;
var $databaseFieldName;
var $databaseFieldKey;
var $quickPrintShow;
var $quickPrintType;
function formFieldBase(&$formObj, $label, $name = NULL) {
$this->_setup($formObj, $label, $name);
}
function _setup(&$formObj, $label, $name) {
//--------------------------------------------------
// Add this field to the form, and return a "UID"
// NOTE: The "ID" is a string for the HTML
$formFieldUid = $formObj->_addField($this);
//--------------------------------------------------
// Get the HTML label
if (function_exists('formLabelOverride')) {
$label = formLabelOverride($label, $this);
}
$this->htmlLabel = $formObj->_htmlEncode($label);
//--------------------------------------------------
// Field name
if ($name === NULL) {
$this->name = human2camel(substr($this->htmlLabel, 0, 20)) . $formFieldUid;
} else {
$this->name = $name;
}
//--------------------------------------------------
// Field configuration
$this->formObj =& $formObj;
$this->formFieldUid = $formFieldUid;
$this->htmlRequiredMark = NULL;
$this->requiredMarkPosition = NULL;
$this->required = false;
$this->id = 'fld' . ucfirst($this->name);
$this->cssClassField = NULL;
$this->cssClassLabel = NULL;
$this->databaseFieldName = NULL;
$this->databaseFieldKey = FORM_DATABASE_FIELD_VALUE;
$this->quickPrintShow = true;
$this->quickPrintType = 'unknown';
$this->quickPrintLabelSuffix = $formObj->quickPrintLabelSuffix;
}
function setRequiredMark($requiredMark) {
$this->htmlRequiredMark = $this->formObj->_htmlEncode($requiredMark);
}
function setRequiredMarkPosition($position) {
if ($position == FORM_REQ_MARK_POS_LEFT || $position == FORM_REQ_MARK_POS_RIGHT || $position == FORM_REQ_MARK_POS_NONE) {
$this->requiredMarkPosition = $position;
} else {
exit('
Invalid required mark position specified - use FORM_REQ_MARK_POS_* constants
');
}
}
function _setDatabaseField($fieldName, $fieldKey = FORM_DATABASE_FIELD_VALUE) {
if (!isset($this->formObj->databaseFields[$fieldName])) {
exit('
Invalid database field "' . html($fieldName) . '" set for field "' . $this->htmlLabel . '"
');
}
$this->databaseFieldName = $fieldName;
$this->databaseFieldKey = $fieldKey;
}
function setDatabaseField($field) {
$this->_setDatabaseField($field);
}
function getDatabaseFieldName() {
return $this->databaseFieldName;
}
function getId() {
return $this->id;
}
function setId($id) {
$this->id = $id;
}
function getQuickPrintType() {
return $this->quickPrintType;
}
function setQuickPrintLabelSuffix($suffix) {
$this->quickPrintLabelSuffix = $suffix;
}
function quickPrintShow($show = NULL) {
if ($show === true || $show === false) {
$this->quickPrintShow = $show;
}
return $this->quickPrintShow;
}
function htmlQuickPrint() {
$htmlOutput = '
' . "\n";
return $htmlOutput;
}
function getName() {
return $this->name;
}
function setFieldClass($cssClass) {
$this->cssClassField = $cssClass;
}
function setLabelClass($cssClass) {
$this->cssClassLabel = $cssClass;
}
function setHtmlLabel($htmlLabel) { // No need for 'setLabel' as this is called on init
$this->htmlLabel = $htmlLabel;
}
function getTextLabel() {
return htmlDecode($this->htmlLabel);
}
function getHtmlLabel($htmlLabel = NULL) {
//--------------------------------------------------
// Required mark position
$requiredMarkPosition = $this->requiredMarkPosition;
if ($requiredMarkPosition === NULL) {
$requiredMarkPosition = $this->formObj->getRequiredMarkPosition();
}
//--------------------------------------------------
// If this field is required, try to get a required
// mark of some form
if ($this->required) {
$htmlRequiredMark = $this->htmlRequiredMark;
if ($htmlRequiredMark === NULL) {
$htmlRequiredMark = $this->formObj->getRequiredMark($requiredMarkPosition);
}
} else {
$htmlRequiredMark = NULL;
}
//--------------------------------------------------
// Return the HTML for the label
return '';
}
function getHtmlField() {
return 'ERROR';
}
function _postProcessValidation() {
}
function setError($error) {
$this->formObj->_setFieldError($this->formFieldUid, $error);
}
function setHtmlError($htmlError) { // Backwards compatibility
$this->setError('HTML:' . $error);
}
function addError($error) {
$this->formObj->_addFieldError($this->formFieldUid, $error);
}
function addHtmlError($htmlError) { // Backwards compatibility
$this->addError('HTML:' . $htmlError);
}
function hasError() {
return $this->formObj->_fieldHasError($this->formFieldUid);
}
}
//--------------------------------------------------
// Child text field
class formFieldText extends formFieldBase {
var $value;
var $minLength;
var $maxLength;
function formFieldText(&$formObj, $label, $name = NULL) {
$this->_setupText($formObj, $label, $name);
}
function _setupText(&$formObj, $label, $name = NULL) {
//--------------------------------------------------
// Perform the standard field setup
$this->_setup($formObj, $label, $name);
//--------------------------------------------------
// Value
$this->value = returnSubmittedValue($this->name, $formObj->getFormMethod());
//--------------------------------------------------
// Default configuration
$this->minLength = 0;
$this->maxLength = 0;
$this->size = NULL;
$this->quickPrintType = 'text';
}
function setMinLength($error, $size = 1) { // Default is "required"
if (strlen($this->value) < $size) {
$this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $size, $error));
}
$this->minLength = $size;
$this->required = ($size > 0);
}
function setMaxLength($error, $size = NULL) {
if ($size === NULL) {
if ($this->databaseFieldName === NULL) {
exit('
You need to call "setDatabaseField", on the field "' . $this->htmlLabel . '"