Form validation!

the code below is working on validating the fields name, email and message.

include("validationclass.php");

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

// instantiate object
$fv = new FormValidator();

$fv->isEmpty("name", "Please enter a name");
$fv->isEmpty("email", "Please enter an email");
$fv->isEmpty("message", "Please enter an message");

Why it would now work below?


$strName = isset($_POST['frmSearch']['name'])?mysql_real_escape_string($_POST['frmSearch']['name']):'';

include("validationclass.php"); 

$fv = new FormValidator();

$fv->isEmpty("frmSearchname", "Please enter a name");

in the isEmpty method i have put the index [‘frmSearch’][‘name’] I have to say frmSearch is the name of the form and the field name.

Since in PHP, the only one form can be submitted at a time, there is no use of form name in PHP. So why do you need to give the form name actually?

I will start by taking the name of the form out!

it will still trow the error when taking the name of the form out.



The operation could not be performed because one or more error(s) occurred.


Please resubmit the form after making the following changes:

    * Please enter a name

While in the first of the two script in the first post will trow this error only if the field hasn’t been filled. But now it will trow the error when the field is empty and when I have assign something to the field.

I have to say this is a form that will retrieve and search information from a as a difference as in a contact form the first example of the two script in the first post where it will submit and send the name, email and message field to my email or input it in the database.

$fv->isEmpty(“frmSearchname”, “Please enter a name”);

Assuming the first parameter is what isEmpty checks, will it EVER be empty???

What i assume is, the isEmpty method of the FormValidator class checks the $_POST array with the first parameter passed with it. So the name, or whatever that is passed as the first parameter, must exists in the submitting form (and in $_POST super global array). If not, then it will throw error.

Taking into account what you guys has suggest to take out the frmSearch parameter I have done this


  $strName = isset($_POST['name'])?mysql_real_escape_string($_POST['name']):'';
 
include("validationclass.php"); 

print($strName);
$fv = new FormValidator();
$fv->isEmpty("name", "Please enter a name");

then a print($strName) and it is displaying the value that I have input in the form field. Which means the value is not empty and that’s what I don’t get, why it is not stopping from trowing the message.

This is also the results of a print_r($_POST);

Array
(
    [name] => polo
    [zipcode] => 
    [state] => 
    [frmSearch] => Array
        (
            [food_types] => Array
                (
                    [0] => 
                )

            [submit] => Submit
        )

)

I have a question the print_r will indicate the input value is passing?

Then we need to see your validator class to check how it does works/checks the values.

validationclass.php

<?php 

// FormValidator.class.inc
// class to perform form validation

class FormValidator
{

// snip

	//
	// methods (private)
	// 
	
	// function to get the value of a variable (field)
	function _getValue($field)
	{
		global ${$field};
		return ${$field};
		
    } 

	
	// check whether input is empty
	function isEmpty($field, $msg)
	{
		$value = $this->_getValue($field);
		if (trim($value) == "")
		{
			$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
			return false;
		}
		else
		{
			return true;
		}

}
		// check whether input is a string
	function isString($field, $msg)
	{
		$value = $this->_getValue($field);
		if(!is_string($value))
		{
			$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
			return false;
		}
		else
		{
			return true;
		}
	}

	
	// check whether input is a number
	function isNumber($field, $msg)
	{
		$value = $this->_getValue($field);
		if(!is_numeric($value))
		{
			$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
			return false;
		}
		else
		{
			return true;
		}
	}

		function isInteger($field, $msg)
	{
		$value = $this->_getValue($field);
		if(!is_integer($value))
		{
			$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
			return false;
		}
		else
		{
			return true;
		}
	}

	// check whether input is a float
	function isFloat($field, $msg)
	{
		$value = $this->_getValue($field);
		if(!is_float($value))
		{
			$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
			return false;
		}
		else
		{
			return true;
		}
	}

	// check whether input is within a valid numeric range
	function isWithinRange($field, $msg, $min, $max)
	{
		$value = $this->_getValue($field);
		if(!is_numeric($value) || $value < $min || $value >
$max)
		{
			$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
			return false;
		}
		else
		{
			return true;
		}
	}
// check whether input is alphabetic
	function isAlpha($field, $msg)
	{
		$value = $this->_getValue($field);
		$pattern = "/^[a-zA-Z]+$/";
		if(preg_match($pattern, $value))
		{
			return true;
		}
		else
		{
			$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
			return false;
		}
	}
// check whether input is a valid email address
	function isEmailAddress($field, $msg)
	{
		$value = $this->_getValue($field);
		$pattern =
"/^([a-zA-Z0-9])+([\\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\\.[a-zA-Z0-9_-]+)+/
";
		if(preg_match($pattern, $value))
		{
			return true;
		}
		else
		{
			$this->_errorList[] = array("field" => $field,
"value" => $value, "msg" => $msg);
			return false;
		}
	}

// check whether any errors have occurred in validation
	// returns Boolean
	function isError()
	{
		if (sizeof($this->_errorList) > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// return the current list of errors
	function getErrorList()
	{
		return $this->_errorList;
	}

	// reset the error list
	function resetErrorList()
	{
		$this->_errorList = array();
	}
// constructor
	// reset error list
	function FormValidator()
	{
		$this->resetErrorList();
	}



}


?>

as I said the isEmpty method is working in the first script in the first post.

Your validator class seems strange. First you need to store the values in a variable and should pass the variable name without the $ to the isEmpty’s first parameter.


$strName = $_POST['name'];
$fv->isEmpty('strName', 'message');

function _getValue($field)
    {
        global ${$field};
        return ${$field};
        
    }

wow

@rajug

Question of the year, first I have to say now it is working, and never so I should be put like that. Why the variable strName is put in there without the $ sign?

@hash

i looked for some sense to it, And it will pull the global variable $field in and get the value and return it back out to be used?