setFormAction($GLOBALS['tplHttpsUrl']);
//$form->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
redirect('./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 $formAction;
var $csrfToken;
var $csrfError;
var $htmlErrors;
var $htmlRequiredMark;
var $requiredMarkPosition;
var $hasRunPostProcessValidation;
var $databaseTableName;
var $databaseTableAlias;
var $databaseRecordSelect;
var $databaseFieldDetails;
var $databaseFieldValues;
var $quickPrintLabelSuffix;
function form() {
//--------------------------------------------------
// Defaults
$this->fields = array();
$this->fieldCount = 0;
$this->formMethod = 'POST';
$this->formAction = (isset($GLOBALS['tplHttpsUrl']) ? $GLOBALS['tplHttpsUrl'] : './');
$this->csrfToken = '';
$this->csrfError = 'The request did not appear to come from a trusted source, please try again.';
$this->htmlErrors = array();
$this->htmlRequiredMark = NULL;
$this->requiredMarkPosition = FORM_REQ_MARK_POS_LEFT;
$this->hasRunPostProcessValidation = false;
$this->databaseTableName = NULL;
$this->databaseTableAlias = NULL;
$this->databaseRecordSelect = NULL;
$this->databaseFieldDetails = array();
$this->databaseFieldValues = array();
$this->quickPrintLabelSuffix = ':';
}
function setFormMethod($method) {
$this->formMethod = $method;
}
function getFormMethod() {
return $this->formMethod;
}
function setFormAction($url) {
//--------------------------------------------------
// Set action
$this->formAction = $url;
//--------------------------------------------------
// Generate a csrfToken if one does not exist.
// When this function is not used, we won't be
// adding CSRF protection.
if (class_exists('cookie')) {
$this->csrfToken = cookie::get('csrfToken');
} else {
$this->csrfToken = returnSubmittedValue('csrfToken', 'COOKIE');
}
if ($this->csrfToken == '') {
$this->csrfToken = rand(1000000, 9999999);
}
if (class_exists('cookie')) {
cookie::set('csrfToken', $this->csrfToken);
} else {
setcookie('csrfToken', $this->csrfToken, 0, '/'); // Would like to set an expiry time of 1 hour, but IE compares expiry date against computer clock (which may be wrong!).
$_COOKIE['csrfToken'] = $this->csrfToken; // If there are multiple forms on the page.
}
}
function getFormAction() {
if ($this->csrfToken != '') {
return urlReplaceVariable('csrfToken', $this->csrfToken, $this->formAction);
} else {
return $this->formAction;
}
}
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() {
//--------------------------------------------------
// Don't run more than once
if ($this->hasRunPostProcessValidation) {
return NULL;
}
//--------------------------------------------------
// CSRF check
if ($this->csrfToken != '') { // Only test if a csrfToken has been generated by setFormAction
$csrfToken = returnSubmittedValue('csrfToken', 'GET');
if ($this->csrfToken != $csrfToken) {
$note = 'C:' . $this->csrfToken . ' != G:' . $csrfToken;
$this->_addFieldError(-1, $this->csrfError, $note);
}
}
//--------------------------------------------------
// Fields
for ($fieldId = 0; $fieldId < $this->fieldCount; $fieldId++) {
$this->fields[$fieldId]->_postProcessValidation();
}
//--------------------------------------------------
// Remember this has been done
$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 resetErrors() {
$this->htmlErrors = array();
}
function addHtmlError($htmlError) { // Backwards compatibility
$this->addError('HTML:' . $htmlError);
}
function setCsrfError($error) {
$this->csrfError = $error;
}
function _fieldHasError($fieldId) {
return ($this->hasRunPostProcessValidation && isset($this->htmlErrors[$fieldId]));
}
function _fieldGetErrors($fieldId) {
if ($this->hasRunPostProcessValidation && isset($this->htmlErrors[$fieldId])) {
return $this->htmlErrors[$fieldId];
} else {
return array();
}
}
function hasError() {
$this->_runPostProcessValidation();
return (count($this->htmlErrors) > 0);
}
function setDatabaseTable($sqlTable, $sqlAlias = NULL, $db = NULL) {
//--------------------------------------------------
// Store
$this->databaseTableName = $sqlTable;
$this->databaseTableAlias = $sqlAlias;
//--------------------------------------------------
// 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 if (strpos(mysql_field_flags($rst, $k), 'set') !== false) {
$type = 'set';
$values = $db->enumValues($sqlTable, $mysqlField->name);
} else {
$type = $mysqlField->type;
$values = NULL;
}
$length = mysql_field_len($rst, $k); // $mysqlField->max_length returns 0
if ($length < 0) {
$db->query('SHOW COLUMNS FROM ' . $sqlTable . ' LIKE "' . $db->escape($mysqlField->name) . '"'); // Backup when longtext returns -1 (Latin) or -3 (UFT8).
if ($row = $db->fetchAssoc()) {
if ($row['Type'] == 'tinytext') $length = 255;
if ($row['Type'] == 'text') $length = 65535;
if ($row['Type'] == 'longtext') $length = 4294967295;
}
}
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->databaseTableName;
}
function getDatabaseTableFields() {
return $this->databaseFields;
}
function setDatabaseRecordSelect($sqlWhere) {
$this->databaseRecordSelect = $sqlWhere;
}
function getDatabaseRecordValues($db = NULL) {
//--------------------------------------------------
// Validation
if ($this->databaseTableName === 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;
}
}
if (count($fields) == 0) {
return false;
}
//--------------------------------------------------
// Get
if ($db == NULL) {
$db = $GLOBALS['db'];
}
$sqlTable = $this->databaseTableName . ($this->databaseTableAlias === NULL ? '' : ' AS ' . $this->databaseTableAlias);
$db->select($sqlTable, $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->databaseTableName === 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'];
}
$sqlTable = $this->databaseTableName . ($this->databaseTableAlias === NULL ? '' : ' AS ' . $this->databaseTableAlias);
if ($this->databaseRecordSelect === NULL) {
$db->insert($sqlTable, $values);
} else {
$db->update($sqlTable, $values, $this->databaseRecordSelect);
}
}
function htmlQuickPrint($group = NULL) {
//--------------------------------------------------
// Backwards compatibility, for an array of fields
// being passed in
if (is_array($group)) {
$fields = $group;
$group = NULL;
} else {
$fields = $this->fields;
}
//--------------------------------------------------
// Create the output
$k = 0;
$htmlOutputMain = '';
$htmlOutputHidden = '';
foreach (array_keys($fields) as $fieldId) {
if ($fields[$fieldId]->quickPrintShow()) {
$fieldGroup = $fields[$fieldId]->getQuickPrintGroup();
if (($group === NULL && $fieldGroup === NULL) || ($group !== NULL && $group == $fieldGroup)) {
$quickPrintType = $fields[$fieldId]->getQuickPrintType();
$k++;
if ($k == 1) {
$fields[$fieldId]->addQuickPrintCssClass('firstChild odd');
} else if ($k % 2) {
$fields[$fieldId]->addQuickPrintCssClass('odd');
} else {
$fields[$fieldId]->addQuickPrintCssClass('even');
}
if ($quickPrintType == 'hidden') {
$htmlOutputHidden .= $fields[$fieldId]->htmlQuickPrint();
} else {
$htmlOutputMain .= $fields[$fieldId]->htmlQuickPrint();
}
}
}
}
return $htmlOutputMain . $htmlOutputHidden;
}
}
//--------------------------------------------------
// 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 $quickPrintGroup;
var $quickPrintCssClass;
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->quickPrintGroup = NULL;
$this->quickPrintCssClass = '';
$this->quickPrintType = 'unknown';
$this->quickPrintInfoHtml = '';
$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 setQuickPrintCssClass($class) {
$this->quickPrintCssClass = $class;
}
function addQuickPrintCssClass($class) {
$this->quickPrintCssClass .= ($this->quickPrintCssClass == '' ? '' : ' ') . $class;
}
function getQuickPrintCssClass() {
$class = 'row ';
$class .= $this->quickPrintType . ' ';
$class .= $this->quickPrintCssClass . ' ';
$class .= $this->name . ' ';
if ($this->hasError()) {
$class .= 'error ';
}
return trim($class);
}
function getQuickPrintShow() {
return $this->quickPrintShow;
}
function setQuickPrintShow($show) {
$this->quickPrintShow = ($show == true);
}
function getQuickPrintType() {
return $this->quickPrintType;
}
function getQuickPrintGroup() {
return $this->quickPrintGroup;
}
function setQuickPrintGroup($group) {
$this->quickPrintGroup = $group;
}
function setQuickPrintLabelSuffix($suffix) {
$this->quickPrintLabelSuffix = $suffix;
}
function setQuickPrintInfo($info) {
$this->quickPrintInfoHtml = html($info);
}
function setQuickPrintInfoHtml($infoHtml) {
$this->quickPrintInfoHtml = $infoHtml;
}
function getQuickPrintInfoHtml($indent = 0) {
if ($this->quickPrintInfoHtml == '') {
return '';
} else {
return ($indent > 0 ? "\n" : '') . str_repeat("\t", $indent) . '' . $this->quickPrintInfoHtml . '';
}
}
function quickPrintShow($show = NULL) { // Backwards compatibility, see getQuickPrintShow/setQuickPrintShow
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
if ($htmlLabel === NULL) {
return '';
} else {
return $htmlLabel;
}
}
function getHtmlField() {
return 'ERROR';
}
function _postProcessValidation() {
}
function setError($error) {
$this->formObj->_setFieldError($this->formFieldUid, $error);
}
function setHtmlError($htmlError) { // Backwards compatibility
$this->setError('HTML:' . $htmlError);
}
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);
}
function getHtmlErrors() {
return $this->formObj->_fieldGetErrors($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 = NULL;
$this->maxLength = NULL;
$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 . '"
');
}
$size = $this->formObj->databaseFields[$this->databaseFieldName]['length'];
}
if (strlen($this->value) > $size) {
$this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $size, $error));
}
$this->maxLength = $size;
}
function setSize($size) {
$this->size = $size;
}
function setValue($value) {
$this->value = $value;
}
function getValue() {
return $this->value;
}
function getValueFormatted() {
return $this->value;
}
function getHtmlField() {
return 'size === NULL ? '' : ' size="' . intval($this->size) . '"') . ($this->cssClassField === NULL ? '' : ' class="' . html($this->cssClassField) . '"') . ' />';
}
function getHtmlFieldHiddenWithValue($value) {
return '';
}
function getHtmlFieldHidden() {
return $this->getHtmlFieldHiddenWithValue($this->value);
}
function _postProcessValidation() {
if ($this->maxLength === NULL) {
exit('
You need to call "setMaxLength", on the field "' . $this->htmlLabel . '"
');
}
}
}
//--------------------------------------------------
// Child hidden field
class formFieldHidden extends formFieldText {
function formFieldHidden(&$formObj, $name) {
//--------------------------------------------------
// Perform the standard field setup
$this->_setupText($formObj, $name, $name);
//--------------------------------------------------
// Default configuration
$this->quickPrintType = 'hidden';
}
function getHtmlLabel($htmlLabel = NULL) {
return '';
}
function getHtmlField() {
return '';
}
function htmlQuickPrint() {
$htmlOutput = '
' . $this->getHtmlField() . "\n";
return $htmlOutput;
}
function _postProcessValidation() {
// Disable max length check.
}
}
//--------------------------------------------------
// Child password field
class formFieldPassword extends formFieldText {
function formFieldPassword(&$formObj, $label, $name = NULL) {
//--------------------------------------------------
// Perform the standard field setup
$this->_setupText($formObj, $label, $name);
//--------------------------------------------------
// Additional field configuration
$this->quickPrintType = 'password';
}
function getHtmlField() {
return 'size === NULL ? '' : ' size="' . intval($this->size) . '"') . ($this->cssClassField === NULL ? '' : ' class="' . html($this->cssClassField) . '"') . ' />';
}
}
//--------------------------------------------------
// Child text-area field
class formFieldTextArea extends formFieldText {
var $textareaRows;
var $textareaCols;
function formFieldTextArea(&$formObj, $label, $name = NULL) {
//--------------------------------------------------
// Perform the standard field setup
$this->_setupText($formObj, $label, $name);
//--------------------------------------------------
// Additional field configuration
$this->textareaRows = 5;
$this->textareaCols = 40;
$this->quickPrintType = 'textarea';
}
function setRows($rows) {
$this->textareaRows = $rows;
}
function setCols($cols) {
$this->textareaCols = $cols;
}
function getHtmlField() {
return '';
}
}
//--------------------------------------------------
// Child number field
class formFieldNumber extends formFieldText {
var $formatErrorSet;
function formFieldNumber(&$formObj, $label, $name = NULL) {
//--------------------------------------------------
// Perform the standard field setup
$this->_setupText($formObj, $label, $name);
//--------------------------------------------------
// Additional field configuration
$this->formatErrorSet = false;
$this->setZeroToBlank = false;
$this->quickPrintType = 'number';
}
function setFormatError($error) {
if ($this->value != '' && !is_numeric($this->value)) {
$this->formObj->_setFieldError($this->formFieldUid, $error);
}
$this->formatErrorSet = true;
}
function setRequiredError($error) {
$this->setMinLength($error);
}
function setMinValue($error, $value) {
if (floatval($this->value) < $value) {
$this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $value, $error));
}
}
function setMaxValue($error, $value) {
if (floatval($this->value) > $value) {
$this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $value, $error));
}
$this->maxLength = (strlen($value) + 6); // Allow for a decimal place, plus an arbitrary 5 digits.
if ($this->size === NULL && $this->maxLength < 20) {
$this->size = $this->maxLength;
}
}
function setZeroToBlank($blank) {
$this->setZeroToBlank = ($blank == true);
}
function getValueFormatted($decimalPlaces = 2) {
if ($this->value == 0 && $this->setZeroToBlank) {
return '';
} else {
return $this->value;
}
}
function _postProcessValidation() {
parent::_postProcessValidation();
if ($this->formatErrorSet == false) {
exit('
You need to call "setFormatError", on the field "' . $this->htmlLabel . '"
');
}
}
}
//--------------------------------------------------
// Child currency field
class formFieldCurrency extends formFieldNumber {
var $formatErrorSet;
var $currencyChar;
function formFieldCurrency(&$formObj, $label, $name = NULL) {
//--------------------------------------------------
// Perform the standard field setup
$this->_setupText($formObj, $label, $name);
//--------------------------------------------------
// Strip currency symbol from input value
$this->setValue($this->value);
//--------------------------------------------------
// Additional field configuration
$this->formatErrorSet = false;
$this->setZeroToBlank = false;
$this->currencyChar = '£';
$this->quickPrintType = 'currency';
}
function setMinValue($error, $value) {
if (floatval($this->value) < $value) {
if ($value < 0) {
$valueText = '-' . $this->currencyChar . number_format(floatval(0 - $value), 2);
} else {
$valueText = $this->currencyChar . number_format(floatval($value), 2);
}
$this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $valueText, $error));
}
$this->minLength = $value;
}
function setMaxValue($error, $value) {
if (floatval($this->value) > $value) {
if ($value < 0) {
$valueText = '-' . $this->currencyChar . number_format(floatval(0 - $value), 2);
} else {
$valueText = $this->currencyChar . number_format(floatval($value), 2);
}
$this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $valueText, $error));
}
$this->maxLength = strlen(floor($value));
$this->maxLength += (intval($this->minLength) < 0 ? 1 : 0); // Negative numbers
$this->maxLength += (function_exists('mb_strlen') ? mb_strlen($this->currencyChar, $GLOBALS['pageCharset']) : strlen($this->currencyChar));
$this->maxLength += (floor((strlen(floor($value)) - 1) / 3)); // Thousand separators
$this->maxLength += 3; // Decimal place char, and 2 digits
if ($this->size === NULL && $this->maxLength < 20) {
$this->size = $this->maxLength;
}
}
function setCurrencyChar($char) {
$this->currencyChar = $char;
}
function setValue($value) {
$this->value = preg_replace('/[^0-9\-\.]+/', '', $value); // Deletes commas (thousand separators) and currency symbols
}
function getValueFormatted($decimalPlaces = 2) {
if ($this->value == 0 && $this->setZeroToBlank) {
return '';
} else if ($this->value < 0) {
return '-' . $this->currencyChar . number_format(floatval(0 - $this->value), $decimalPlaces);
} else {
return $this->currencyChar . number_format(floatval($this->value), $decimalPlaces);
}
}
}
//--------------------------------------------------
// Child email field
class formFieldEmail extends formFieldText {
var $formatErrorSet;
function formFieldEmail(&$formObj, $label, $name = NULL) {
//--------------------------------------------------
// Perform the standard field setup
$this->_setupText($formObj, $label, $name);
//--------------------------------------------------
// Additional field configuration
$this->formatErrorSet = false;
$this->quickPrintType = 'email';
}
function setFormatError($error) {
if ($this->value != '' && !isemail($this->value)) {
$this->formObj->_setFieldError($this->formFieldUid, $error);
}
$this->formatErrorSet = true;
}
function _postProcessValidation() {
parent::_postProcessValidation();
if ($this->formatErrorSet == false) {
exit('
You need to call "setFormatError", on the field "' . $this->htmlLabel . '"
');
}
}
}
//--------------------------------------------------
// Child URL field
class formFieldUrl extends formFieldText {
var $formatErrorSet;
function formFieldUrl(&$formObj, $label, $name = NULL) {
//--------------------------------------------------
// Perform the standard field setup
$this->_setupText($formObj, $label, $name);
//--------------------------------------------------
// Additional field configuration
$this->formatErrorSet = false;
$this->quickPrintType = 'url';
}
function setFormatError($error) {
if ($this->value != '') {
$parts = @parse_url($this->value);
if ($parts === false || !isset($parts['scheme']) || !isset($parts['host'])) {
$this->formObj->_setFieldError($this->formFieldUid, $error);
}
}
$this->formatErrorSet = true;
}
function setAllowedSchemes($error, $schemes) {
if ($this->value != '') {
$parts = @parse_url($this->value);
if (isset($parts['scheme']) && !in_array($parts['scheme'], $schemes)) {
$this->formObj->_setFieldError($this->formFieldUid, $error);
$this->formObj->_setFieldError($this->formFieldUid, $error, 'Scheme: ' . $parts['scheme']);
}
}
}
function _postProcessValidation() {
parent::_postProcessValidation();
if ($this->formatErrorSet == false) {
exit('
You need to call "setFormatError", on the field "' . $this->htmlLabel . '"
');
}
}
}
//--------------------------------------------------
// Child post code field
class formFieldPostCode extends formFieldText {
var $formatErrorSet;
function formFieldPostCode(&$formObj, $label, $name = NULL) {
//--------------------------------------------------
// Perform the standard field setup
$this->_setupText($formObj, $label, $name);
//--------------------------------------------------
// Additional field configuration
$this->maxLength = 8; // Bypass required setMaxLength call, and to set the
$this->formatErrorSet = false;
$this->quickPrintType = 'postcode';
}
function setFormatError($error) {
if ($this->value != '') {
$postcodeClean = formatBritishPostcode($this->value);
if ($postcodeClean === NULL) {
$this->formObj->_setFieldError($this->formFieldUid, $error);
}
}
$this->formatErrorSet = true;
}
function setRequiredError($error) {
if ($this->value == '') {
$this->formObj->_setFieldError($this->formFieldUid, $error);
}
$this->required = ($error !== NULL);
}
function getValue() {
return formatBritishPostcode($this->value);
}
function _postProcessValidation() {
parent::_postProcessValidation();
if ($this->formatErrorSet == false) {
exit('
You need to call "setFormatError", on the field "' . $this->htmlLabel . '"
');
}
}
}
//--------------------------------------------------
// Child check box field
class formFieldCheckBox extends formFieldText {
var $textValueTrue;
var $textValueFalse;
function formFieldCheckBox(&$formObj, $label, $name = NULL) {
//--------------------------------------------------
// Perform the standard field setup
$this->_setupText($formObj, $label, $name);
//--------------------------------------------------
// Additional field configuration
$this->value = ($this->value == 'true');
$this->maxLength = -1; // Bypass the _postProcessValidation on the text field (not used)
$this->quickPrintType = 'check';
$this->quickPrintInputFirst = false;
$this->textValueTrue = NULL;
$this->textValueFalse = NULL;
}
function quickPrintInputFirst($first = NULL) {
if ($first === true || $first === false) {
$this->quickPrintInputFirst = $first;
}
return $this->quickPrintInputFirst;
}
function setTextValues($true, $false) {
$this->textValueTrue = $true;
$this->textValueFalse = $false;
}
function setRequiredError($error) {
if ($this->value !== true) {
$this->formObj->_setFieldError($this->formFieldUid, $error);
}
$this->required = ($error !== NULL);
}
function getHtmlField() {
return 'value ? ' checked="checked"' : '') . ($this->cssClassField === NULL ? '' : ' class="' . html($this->cssClassField) . '"') . ' />';
}
function htmlQuickPrint() {
if ($this->quickPrintInputFirst) {
$htmlOutput = '