Jump to content

Recommended Posts

This isnt something that has a link, but you'll have to run the script yourself on your local machine.

 

I am creating a validator class that will handle all of the user input before the database.

 

I have a few methods in the class, i am looking for testing of these methods and suggestions for more.

 

I feel that it is pretty simple to use. the only sidenote is that i use a function called tag that isnt included in the code below

 

<?php
function tag($content, $tag, $atts = ''){
return ($content) ? "<". $tag ." ". $atts .">". $content. "</". $tag. ">\n" : '';
}


require_once('../../functions/basic.php');
require_once('../../classes/class._validate.php');

$array = array(
array('method' => 'email', 'value' => 'testtest.com', 'name' => 'email', 'msg' => 'lol @ you. dumbass'), 
array('method' => 'password', 'value' => 'mark', 'name' => 'password'),
array('method' => 'number', 'value' => 'r', 'name' => 'numberd', 'msg' => 'fuk face this is not a number :/')
);

$x = new validate($array);
$xy = $x->process();
if(!$x->_error){
echo "No errors.<pre>", print_r($xy), "</pre>";
}else{
echo $xy;
}
?>

 

In this example you define a multi dimensional array of elements where you define the method that you want to use and pass the post variables through it. you initiate the class then define a variable to hold the result from the process() method.

 

you then check to see if the _error property of the class is set, if it is you can handle the error message anyway you like if it isnt, you can then use the returned vars with the 'name' attribute being the key in the array so they have to be unique.

 

i hope this makes sense to yall. check it out for me. thanks

 

<?php
session_start();
/*******************************************************************
*
* File: class._validate.php
*
* 
*
********************************************************************/

class validate{

public $_data;
public $_dataSubArr;
public $_retArr 	= array();
public $_error 		= false;
public $_errorMsg 	= array();
public $_errorWrap	= "ul";
public $_errorSel	= "li";

public function __construct($data){
	$this->_data = $data;
	foreach($this->_data as $key => $subArr){
		$this->set_method($subArr);
	}
}

public function addToArr($key, $val){
	$this->_retArr[$key] = mysql_escape_string($val);
}


public function process(){
	if($this->_error){
		$msg = '';
		foreach($this->_errorMsg as $key => $val){
			$msg .= tag($val, $this->_errorSel);
		}
		return tag($msg, $this->_errorWrap);
	}else{
		return $this->_retArr;
	}
}


public function set_method($arr = ''){
	switch($arr['method']){
		case 'password':
			$this->_dataSubArr = $arr;
			return $this->password();
			break;
		case 'email':
			$this->_dataSubArr = $arr;
			return $this->email();
			break;
		case 'compare':
			return $this->compare();
			break;
		case 'number':
			return $this->number();
			break;
	}
}


public function number($arr = ''){
	$value			= '';
	$name			= '';
	$msg			= 'The value is not numeric.';

	$a				= ($arr != '') ? $arr : $this->_dataSubArr;

	foreach($a as $key => $setVal){
		$$key = $setVal;
	}

	if(is_numeric($value)){
		$this->addToArr($name, $value);
	}else{
		$this->_error 		= true;
		$this->_errorMsg[] 	= $msg;
	}
}

public function compare($arr = ''){
	$value1 		= '';
	$value2			= '';
	$msg			= 'Values do not match.';

	$a				= ($arr != '') ? $arr : $this->_dataSubArr;

	foreach($a as $key => $setVal){
		$$key = $setVal;
	}

	if($value1 === $value2) return true;
	return false;
}


public function password($arr = ''){
	$value 				= '';
	$name				= '';
	$encryption 		= true;
	$encryption_type	= 'sha1';
	$compare			= false;
	$msg				= 'Your passwords do not match.';

	$a					= ($arr != '') ? $arr : $this->_dataSubArr;

	foreach($a as $key => $setVal){
		$$key = $setVal;
	}

	$res = ($compare != false) ? $this->compare($value, $compare) : true;

	if($encryption && $res){
		switch($encryption_type){
			case 'sha1':
				$this->addToArr($name, sha1($value));
				break;
			case 'md5':
				$this->addToArr($name, md5($value));
				break;
		}
	}elseif($res){
		$this->addToArr($name, $value);
	}else{
		$this->_error 		= true;
		$this->_errorMsg[] 	= $msg;
	}
}


public function email($arr = ''){
	$value 			= '';
	$setLength		= false;
	$msg			= 'This is not a valid e-mail address.';

	$a				= ($arr != '') ? $arr : $this->_dataSubArr;

	foreach($a as $key => $setVal){
		$$key = $setVal;
	}

	$len = ($setLength == false) ? '' : '{'. $setLength .'}';

	if(!eregi("^[_a-z0-9-]". $len ."+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $value)){
		$this->_error 		= true;
		$this->_errorMsg[] 	= $msg;
	}else{
		$this->addToArr($name, $value);
	}		
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/38849-validator-class/
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.