<?php
# =================================================================== #
#
# iMarc PHP Library
# Copyright 2002, iMarc, LLC
#
# @version: 1.4
# @last_update: 2003-01-01
# @description: Form Validation Functions
#
# @changes: v1.4 - Added isset() when validating (compatibale with PHP error notices)
#
# =================================================================== #
/*
METHODS:
validate_fields() - Validates a comma-separated string of $required_fields
create_error() - Private class function to handle errors
*/
# ------------------------------------------------------------------- #
# VALIDATOR CLASS
# ------------------------------------------------------------------- #
class validator {
var $error, $error_message; // (string) HTML formatted error message
var $error_array = array(); // (array) Error
var $error_code;
# ----------------------------------- #
# FUNCTION: validate_fields
# DESCRIPTION: Validates a comma-separated string of $required_fields
#
# ARGS: $required_fields (string) comma separated string of required form field names
#
# RETURNS: TRUE if all form fields are not NULL, FALSE if at least one fields is NULL.
# ----------------------------------- #
function validate_fields($required_fields='') {
if (!$required_fields) {
return true;
}
$__errors = array();
// Delete all spaces from the field names and place the them in an array.
$__fields = explode (",", str_replace(" ", "", $required_fields));
foreach ($__fields as $__current_field) {
if (trim($__current_field)) {
if (strstr($__current_field, "||")) {
/* * * * "OR" fields * * * */
// this_field||that_field - double pipe separated field names will check <this_field> or <that_field>
$__error = false;
$__no_error = false;
$__sub_fields = explode("||", $__current_field);
foreach ($__sub_fields as $__current_sub_field) {
$__current_sub_field = trim($__current_sub_field);
settype($_REQUEST[$__current_sub_field], "string");
if (!$__no_error && isset($_REQUEST[$__current_sub_field]) && !strlen(trim($_REQUEST[$__current_sub_field]))) {
$__error = true;
} else {
$__no_error = 1;
$__error = false;
}
}
if ($__error) {
$__errors[] = $__current_field;
}
} else {
/* * * * Regular fields * * * */
// This separates regular single fields and makes them global variables
$__current_field = trim($__current_field);
settype($_REQUEST[$__current_field], "string");
if (isset($_REQUEST[$__current_field]) && !strlen(trim($_REQUEST[$__current_field]))) {
$__errors[] = $__current_field;
}
}
}
}
if (count($__errors)) {
$this->create_error($__errors, "validate_fields");
return FALSE;
} else {
return TRUE;
}
}
/* Private */
# ----------------------------------- #
# FUNCTION: create_error
# DESCRIPTION: Creates error messages
#
# ARGS: $error (mixed) error message[s]
# $type (string) type of error
#
# RETURNS: VOID
# Sets $obj->error and $obj->error_array
# ----------------------------------- #
function create_error($error, $type='') {
$this->error = ereg_replace("<br>$", "", $this->error);
if ($type == "validate_fields") {
$this->error_code = 1070;
foreach ($error as $v) {
$i = 1;
$r .= " • ";
$v_array = explode("||", $v);
foreach ($v_array as $c) {
if (trim($c)) {
if ($i > 1) { $r .= " <b>veya</b> "; }
$missing_fields[] = $c;
$r .= ucwords(eregi_replace("_", " ", $c));
$i++;
}
}
$r .= "<br>\n";
}
$this->error .= $r;
$this->error_array['missing_fields'] = $missing_fields;
} elseif ($type == "message") {
if (!$this->error_array['message']) {
$this->error .= "<b>The following errors occured:</b><br>\n";
}
$this->error .= " • " . $error . "<br>\n";
$this->error_array['message'][] = $missing_fields;
}
// $this->error .= "<br>";
$this->error_message = $this->error;
}
}
/*
<readme>
Validator Class is a PHP object that can be used to validate
the presence of HTML form data.
By "validating", the function simply checks if a variable is
NOT NULL. The class is intended to be called AFTER the end-user
has submitted an HTML form.
The class is first initiated, then the validate_fields function
is called by passing the field names of all the required form
fields. If any of the variables (field names) are NULL the
function returns FALSE, along with an error message of which
fields are null. If all the variables are NOT NULL, the function
returns TRUE.
# --------------------------------------------------------------- #
# VALIDATOR CLASS USAGE
# --------------------------------------------------------------- #
- Start a new instance of the object:
$my_validator = new validator();
- Check for the presence of data in one variable named $my_variable
$my_validator->validate_fields("my_variable");
- Check for the presence of data in three variables
named $first_name, $last_name, and $email
$my_validator->validate_fields("first_name, last_name, email");
- Check for the presence of data in at least one check box. There
are 3 checkboxes on the form ($ch_1, $ch_2, and $ch_3)
$my_validator->validate_fields("ch_1||ch_3||ch_3");
- Check for the presence of data in at least one check box,
AND each of 2 text fields:
$my_validator->validate_fields("ch_1||ch_3||ch_3, text_1, text_2");
- Printing Errors
if (!$my_validator->validate_fields("last_name, email")) {
echo $my_validator->error;
} else {
...
}
# --------------------------------------------------------------- #
# VALIDATOR CLASS SET UP
# --------------------------------------------------------------- #
The only set up is to include this file on the page that you're
using it
</readme>
*/
?>