Return to Snippet

Revision: 24007
at February 18, 2010 11:38 by nate63179


Initial Code
/** 
 * ==============================================
 *	Generic form processing code
 * ==============================================
*/
$form_errors = array();
$form_errorlist = false;

/**
 * Adds an error to our $form_errors variable
 *
 * @return void
 **/
function add_error($error)
{
	global $form_errorlist, $form_errors;
	$form_errorlist = true;
	$form_errors[] = $error;
}

/**
 * Called by validate_form
 * @param	$method
 * 	the container holding our form data $_GET or $_POST
 * @return void
 **/
function _process_form($method)
{
	if (function_exists("process_form")) {
		$data = $method;
		
		// clean up any elements not included within 
		foreach($data as $key => $val) {
			if (preg_match("/(submit|required)|(_desc$)/i", $key) == 1) {
				unset($data[$key]);
			}
		}
		// call process_form(), pass clean form values
		process_form($data);
	}
}

/**
 * Generic validation steps
 *
 * @return bool
 **/
function validate_form($method)
{
	$process = true; // assume true until proven otherwise
	if (!isset($method['required'])) {
		add_error("Required hidden element 'required' missing.");
		$process = false;
	} else {
		$required_fields = explode(',', $method['required']);
		
		// Scan for empties
		foreach($required_fields as $val) {
			if (empty($method[$val])) {
				// Required element is missing data
				if (isset($method[$val . "_desc"])) {
					$errormsg = "The required field '" . $method[$val . "_desc"] . "' was empty.";
				} else {
					$errormsg = "The required field '$val' was emtpy.";
				}
				add_error($errormsg);
				$process = false;
			}
		}
		
		// Call dynamic validator methods
		foreach($method as $key => $val) {
			if (preg_match("/(submit|required)|(_desc$)/i", $key) != 1) {
				$validator_function = $key . "_validate"; // the name of the function which will be called for validation
				if (function_exists($validator_function)) {
					if (!isset($method[$key . "_desc"])) {
						$result = $validator_function($val, $key);
					} else {
						$result = $validator_function($val, $method[$key . "_desc"]);
					}
					
					if ($result !== true) {
						add_error($result);
						$process = false;
					}
				}
			}
		}
	}
	
	if ($process) {
		_process_form($method);
		return true;
	}
	return false;
}

/**
 * Returns the value stored in $method or an alternate value when not available
 * @param $field_name
 * 	The name value of the input element
 * @param	$alternate
 *  Text to be displayed if value field_name value is undefined
 * @return String
 * @author Nate Miller
 **/
function write_form_var($field_name, $alternate)
{
	global $method;
	$out = $alternate;
	if ($method[$field_name]) {
		$out = $method[$field_name];
	}
	return $out;
}

/** 
 * ==============================================
 *	Custom validation logic
 * ==============================================
*/

$method = &$_GET;
if (isset($method['submit'])) {
	validate_form($method);
}

/**
 * Validates an email address
 * @param	$data
 * 	the submitted value
 * @param $desc
 * 	the field descriptor - human friendly name of failed field
 *
 * @return bool | error string
 * @author Nate Miller
 **/
function email_validate($data, $desc)
{
	$email_reg = "/^[a-z0-0\._-]+@+[a-z0-0\.-]+\.+[a-z]{2,3}$/i";
	if (preg_match($email_reg, $data) != 1)
		return "The '$desc' field in invalid.";
	return true;
}

/**
 * Controls called by validate_form when form is sucessful
 *
 * @return void
 * @author Nate Miller
 **/
function process_form()
{
	echo "Submit dude";
}

Initial URL


Initial Description
A handy, generic form processor which validates presence of fields marked in a hidden value, like: <input>. Easy to override and customize as needed. 

Adapted from a script found in PHP 5 Unleashed.

Initial Title
PHP: Generic form processing script

Initial Tags
php, forms

Initial Language
PHP