/** * Arguments : $conditions - An array containing all the validaiton information. * $show(Integer) - The value given here decides how the data should be returned - or printed.[OPTIONAL] * 1 = Prints the errors as an HTML List * 2 = Return the errors as a string(HTML list) * 3 = Return the errors as an array * 4 = Return the errors as an array with field name as the key. * Defaults to 1 * Super powerful validaiton script for form fields. I may make this a class to do both serverside and * client side validaiton - both in the same package * :TODO: This function is not fully tested. It is not even partaily tested. * :TODO: Documentation needed desperatly * :TODO: Change this function to a class. The first argument - $conditions is an array with all the validaiton rule Each element of the array is one rule. Each rule is an associative array. The following keys are supported name : The name of the field that should be checked. ($_REQUEST['date'] - here the name is 'date') is : What sort data should MAKE AN ERROR. If the given type is found as the field value, an error will be raised. Example 'empty' title : The human friendly name for the field (eg. 'Date of Birth') error : The message that should be shown if there is a validation error value : The programmer provided value. Some rules must have an additional value to be matched against. For example the '<' condition must have a value - the user inputed value and the value given in this index will be compared when : This is a method to short-circut the validation. If this is false, or '0' validaiton will NOT take place. The rule will just be ignored. Example : $conditions = array( array( 'name' => 'username', 'is' => 'empty', 'error' => 'Please provide a valid username' ), array( 'name' => 'username', 'is' => 'length<', 'value' => 3, 'error' => 'Make sure that then username has atleast 3 chars' ) ) */ function check($conditions, $show = 1) { $errors = array(); $field_errors = array(); foreach ($conditions as $cond) { unset($title, $default_error, $error, $when, $input, $is, $value, $name, $value_field); extract($cond); if (!isset($title)) { $title = format($name); } if (!isset($name)) { $name = unformat($title); } $input = ''; if (!empty($_REQUEST[$name])) { $input = $_REQUEST[$name]; } if (isset($value_field)) { $value = $_REQUEST[$value_field]; } $default_error = "Error in '{$title}' field!"; if (!isset($error)) { $error = $default_error; } if (isset($when)) { if ($when === 0 or $when === false) { //Ok - don't validate this field - ignore erros if any continue; } else { if ($when != "") { //When error $errors[] = $error; } } } switch ($is) { case 'empty': if (!$input) { if ($error == $default_error) { $error = "The {$title} is not provided"; } $field_errors[$name][] = $error; } break; case 'not': if ($error == $default_error) { $error = "The {$title} should be '{$value}'"; } if ($input != $value) { $field_errors[$name][] = $error; } break; case 'equal': if ($error == $default_error) { $error = "The {$title} should field must not be '{$value}'"; } if ($input == $value) { $field_errors[$name][] = $error; } break; //Numeric Checks //Numeric Checks case '>': case 'greater': if ($input > $value) { $field_errors[$name][] = $error; } break; case '<': case 'lesser': if ($input < $value) { $field_errors[$name][] = $error; } break; //Length Checks //Length Checks case 'length<': if (strlen($input) < $value) { $field_errors[$name][] = $error; } break; case 'length>': if (strlen($input) > $value) { $field_errors[$name][] = $error . $value . ' : ' . strlen($input); } break; case 'nan': case 'not_number': //Warning: Decimals will get through if ($input and !is_numeric($input)) { $field_errors[$name][] = "The " . $title . " should be a number"; } break; case 'not_email': //If the field does not match the email regexp, an error is shown if (!preg_match('/^[\\w\\-\\.]+\\@[\\w\\-\\.]+\\.[a-z\\.]{2,5}$/', $input)) { if ($title) { $error = "The " . $title . " should be a valid email address"; } else { $error = "Invalid Email address provided"; } $field_errors[$name][] = $error; } break; case 'has_weird': //Check for weird chars if (!preg_match('/^[\\w\\-]*$/', $input)) { if ($title) { $error = "The " . $title . " should not have weird characters"; } else { $error = "Weird characters where found in the input"; } $field_errors[$name][] = $error; } break; case 'not_name': //Check for chars that cannot appear in a title if (!preg_match("/^[\\w\\'\\(\\)\\,\\.\\/ ]*\$/", $input)) { if ($title) { $error = "The " . $title . " has invalid characters"; } else { $error = "Invalid characters where found in the input"; } $field_errors[$name][] = $error; } break; //RegExp //RegExp case 'dont_match': case 'not_match': case '!match': if (!preg_match("/{$value}/", $input)) { $field_errors[$name][] = $error; } break; case 'match': if (preg_match("/{$value}/", $input)) { $field_errors[$name][] = $error; } break; } } //Put all errors into one array if ($field_errors) { foreach ($field_errors as $name => $arr) { $errors = array_merge($errors, $arr); } $errors = array_values(array_diff($errors, array(''))); } if (!$errors) { return ''; } $error_message = "<ul class='validation-errors'>\n<li>"; $error_message .= implode("</li>\n<li>", $errors); $error_message .= "</li>\n</ul>"; if ($show == 1) { //Just show the errors as one list if the user wants it so print $error_message; } else { if ($show == 2) { //Return the errors as a string(HTML list) return $error_message; } else { if ($show == 3) { //Return the errors as an array return $errors; } else { //Return the errors as a array with field information return $field_errors; } } } }
/** * Add a field that can only be seen when the data is being listed(list action). This field don't have to be in the database * Example: $admin->addListingField('User Posts','"<a href=\'$row[url]\'>View All Post of this User</a>"'); */ function addListingField($title, $data) { if ($this->action == 'list' or $this->action == 'add_save' or $this->action == 'edit_save') { $this->addField(unformat($title), $title, 'virtual', array(), array('html' => $data)); } $this->setListingFields(); }