Ummm.....
Here is my FormValidation class and an example script that I have so you can see how to use the class.
Ignore the XML transformation which you can replace with your own HTML FORMs etc.
FormValidation
PHP Code:
class FormValidation {
var $data;
var $flag;
var $errors;
var $counter;
var $is_errors;
/**
* has a FORM been sent yet ?
*/
function CheckFormStatus() {
return (isset($_POST["_SubmitForm_"]))? true:false;
}
/**
* get FORMs $_POST data
*/
function GetFormPostData() {
$this -> data = array_values($_POST);
}
/**
* get an FORM fields user input
*/
function GetFormUserInput($num) {
return $this -> data[$num];
}
/**
* clear class variables
*/
function InitFormVars() {
$this -> flag = 1;
$this -> counter = 0;
unset($this -> errors);
unset($this -> is_errors);
$this -> errors = array();
$this -> is_errors = array();
}
/**
* a FORM has been validated yet ?
*/
function FormValidated() {
return (in_array(0, $this -> is_errors))? 0:1;
}
function SetOneFormError($num) {
$this -> is_errors[$num] = (int) 0;
}
function GetOneFormError($num) {
return $this -> is_errors[$num];
}
/**
* return what errors if any
*/
function GetFormErrorMessages() {
$str = '';
foreach($this -> errors as $index) {
if(is_array($index) && !$index['value']) {
$str .= (string) $index['message']. '<br />';
}
}
return $str;
}
function FormInputIsAlpha($msg) {
$this -> is_errors[$this -> counter] = (ereg("^[a-zA-Z ]+$", $this -> data[$this -> counter]))? 1:0;
$this -> FormDumpErrors($msg);
$this -> counter++;
}
function FormInputIsWebAddress($tag, $msg) {
return true;
}
function FormInputIsCurrency($msg) {
$this -> is_errors[$this -> counter] = (ereg("^[0-9]+(\.[0-9]{2})$", $this -> data[$this -> counter]))? 1:0;
$this -> FormDumpErrors($msg);
$this -> counter++;
}
function FormInputIsNumeric($msg) {
$this -> is_errors[$this -> counter] = (ereg("^[0-9 ]+$", $this -> data[$this -> counter]))? 1:0;
$this -> FormDumpErrors($msg);
$this -> counter++;
}
function FormInputIsEmail($msg) {
$this -> is_errors[$this -> counter] = (ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $this -> data[$this -> counter]))? 1:0;
$this -> FormDumpErrors($msg);
$this -> counter++;
}
function FormInputIsAlphaNumeric($msg) {
$this -> is_errors[$this -> counter] = (ereg("^[a-zA-Z0-9 ]+$", $this -> data[$this -> counter]))? 1:0;
$this -> FormDumpErrors($msg);
$this -> counter++;
}
function FormInputIsString($msg) {
$this -> is_errors[$this -> counter] = (ereg("^[a-zA-Z0-9: \.\,\?\!\n\r]+$", $this -> data[$this -> counter]))? 1:0;
$this -> FormDumpErrors($msg);
$this -> counter++;
}
function FormInputDefaultDropdown($msg) {
$this -> is_errors[$this -> counter] = (!$this -> data[$this -> counter] == (int) 0)? 1:0;
$this -> FormDumpErrors($msg);
$this -> counter++;
}
function FormDumpErrors($msg) {
if(!$this -> is_errors[$this -> counter]) {
# there has been an error for this FORM field
$this -> errors[$this -> counter] = array('message' => $msg);
}
else {
# no errors, so retain users FORM field input value
$this -> errors[$this -> counter] = array('value' => $this -> data[$this -> counter]);
}
}
}
First of all, the FORM used by this peice of script has following (in order) INPUTs:
Forename
Surname
Username
Password
Repeat Password
When you use the above class remember that you need to check the INPUTs as in the order you have them within the FORM. You cannot for example, look at Password before you look at Forename etc - simplifys the scripting you see...
PHP Code:
function DoAddUser($ob, $db) {
# create a new instance of FormValidation class
$form = new FormValidation;
if(!$form -> CheckFormStatus()) {
# request user to add a new user since FORM wasn't sent
#
# note point one:
# place your FORM here - initial (first) display only
# start here
$ob -> SetXmlFile('add-user.xml');
$ob -> AppendText2XmlFile('<navigate />', MakeMenu($ob));
$ob -> MakeXmlString();
# end here
}
else {
$form -> GetFormPostData();
$form -> InitFormVars();
# check FORM user inputs for valid characters
# you can also add error message within the quotes of this method execution if an error actually exists
$form -> FormInputIsAlpha(''); /** forename **/
$form -> FormInputIsAlpha(''); /** surname **/
$form -> FormInputIsAlphaNumeric(''); /** username **/
$form -> FormInputIsAlphaNumeric(''); /** password **/
$form -> FormInputIsAlphaNumeric(''); /** password repeat **/
# need to confirm that both first and repeat passwords are a match
if($form -> GetFormUserInput(3) != $form -> GetFormUserInput(4)) {
# no match found so set errors for an invalid user input
$form -> SetOneFormError(3);
$form -> SetOneFormError(4);
}
if(!$form -> FormValidated()) {
# invalid user inputs
#
# point two:
# this is where you re-display your FORM though with valid (only) INPUTs which you grab from the class
# using ...$form -> GetFormUserInput(...element);
$ob -> SetXmlFile('add-user-error.xml');
$ob -> AppendText2XmlFile('<navigate />', MakeMenu($ob));
# begin to put valid inputs back to FORM again
if($form -> GetOneFormError(0) == (int) 1) {
# found no invalid inputs for forename
$ob -> AppendText2XmlString('<data />', $form -> GetFormUserInput(0));
}
if($form -> GetOneFormError(1) == (int) 1) {
$ob -> AppendText2XmlString('<data-1 />', $form -> GetFormUserInput(1));
}
if($form -> GetOneFormError(2) == (int) 1) {
$ob -> AppendText2XmlString('<data-2 />', $form -> GetFormUserInput(2));
}
if($form -> GetOneFormError(3) == (int) 1 && $form -> GetOneFormError(4) == (int) 1) {
# only restore if both password and password repeat match
$ob -> AppendText2XmlString('<data-3 />', $form -> GetFormUserInput(3));
$ob -> AppendText2XmlString('<data-4 />', $form -> GetFormUserInput(4));
}
# need to dynamically create SELECT box values used for access level of new user
$ob -> SetXmlBuffer();
# read in currently logged user's access level
$Access = (int) $_SESSION['OffManager']['UserAccess'];
# find true limit of access if user has 'global' status from config file
$Access = ($Access == (int) 0)? MAX_ACCESS_LIMIT:$Access;
$XmlFragment = '';
for($a = 1;$a < $Access;$a++) {
$ob -> AddXmlTag2Buffer('form-option', $a, array('value' => $a));
$XmlFragment .= $ob -> GetXmlBuffer(1);
}
$ob -> AppendText2XmlString('<data-5 />', $XmlFragment);
$ob -> MakeXmlString();
# end of re-display of FORM here
}
else {
# all inputs have valid characters
if($db -> QueryDbase("INSERT INTO userbase (date, username, password, accesslevel, forename, surname) VALUES (now(), '". $form -> GetFormUserInput(2) ."', PASSWORD('". $form -> GetFormUserInput(3) ."'), '". $form -> GetFormUserInput(5) ."', '". $form -> GetFormUserInput(0) ."', '". $form -> GetFormUserInput(1) ."')")) {
# database insertion was okay else report/log error for duplicate entry
$ob -> SetXmlFile('add-user-ok.xml');
$ob -> AppendText2XmlFile('<navigate />', MakeMenu($ob));
$ob -> MakeXmlString();
}
}
}
}
PHP Code:
function RedirectJS($url) {
?>
<script language="javascript1.2" type="text/javascript">
window.location = "<? echo($url); ?>";
</script>
<?php
}
Since you are not using XML etc you will need to make changes to the above script; or re-write your own based on mine.
On the section of script I have:
PHP Code:
# begin to put valid inputs back to FORM again
if($form -> GetOneFormError(0) == (int) 1) {
# found no invalid inputs for forename
$ob -> AppendText2XmlString('<data />', $form -> GetFormUserInput(0));
You could use for example:
PHP Code:
# begin to put valid inputs back to FORM again
$forename = ''; /** reset initially as could be an error **/
if($form -> GetOneFormError(0) == (int) 1) {
# found no invalid inputs for forename
$forename = (string) $form -> GetFormUserInput(0));
And then further down - still within the condition - put $forename within the VALUE part of the INPUT - if you follow ?
This way it'll tidy up your database insertion also - plus you get a top validation class to boot as well 8)
Any problem what so ever then get back to this post - only too glad to help ok ?
Bookmarks