Class de validations de date, entier, string,...

Soyez le premier à donner votre avis sur cette source.

Vue 3 621 fois - Téléchargée 180 fois

Description

Ma classe a pour but de regrouper les validation des string des chiffres des dates ou meme des objets.
L'idée est de ne pas se limiter a un test (format,obligatoire, limite) mais de les regrouper.
Une sous classe permet de définir les différents format de test utiles. Ceci a pour but de ne pas surcharger la class mère "commune". On peut aussi compléter ou surcharger les tests dans cette sous classe.
La validation retourne un bool et empile les différentes erreurs rencontrées dans un tableaux d erreur

Source / Exemple :


<?//----------------------- classe mère --------------------------------------
class Validation_Data_Exception extends Exception {

	const ERROR_FORMAT_NOT_DEFINED = 'format type not defined';
	const ERROR_FORMAT_ILL_DEFINED = 'format ill-defined';
	const ERROR_REGEX_TYPE_NOT_DEFINED = 'regex type not defined';
	const ERROR_LIMIT_TYPE_NOT_DEFINED = 'limit type not defined';
	const ERROR_LIMIT_MIN_MAX = 'limit min max error';

	public function __construct($msg, $code = 0) {
		parent::__construct($msg, $code);
	}
}

abstract Class Validation_Data
{
	//formats contain tests_type
	protected $formats;
	
	//tests_type contain tests
	protected $test_type = array("textfield1"=>array("mandatory","regex","limit"));
	//mandatory(true|false) 
	//regex('Alphanum'|'integer'|'float'|'date_ch'|'email')
	//limit('num'|'string' ; (integer|false ; true|false ; integer|false ; true|false))
	
	//validation errors
	protected $errors;
	
	protected function __construct ($formats) {
		$this->formats = $formats;
		$this->errors = array();
	}

	function valid_format($format,$value) {
		$return = true;
		$tests = $this->get_tests($format);
		$tests_values = $this->get_tests_values($format);
		if(count($tests) == count($tests_values)) {
			for($i=0;$i<count($tests);$i++) {
				$temp_return = $this->valid_test($tests[$i],$tests_values[$i],$value);
				$return = $return && $temp_return["return"];
				if($temp_return["break"]) {
					break;
				}
			}
			return $return;
		} else {
			throw new Validation_Data_Exception (Validation_Data_Exception::ERROR_FORMAT_ILL_DEFINED);
		}
	}
	
	protected function get_test_type($format) {
		if(isset($this->formats[$format][0])) {
			$test_type = $this->formats[$format][0];
			return $test_type;
		} else {
			throw new Validation_Data_Exception (Validation_Data_Exception::ERROR_FORMAT_NOT_DEFINED);
		}
	}
	
	protected function get_tests($format) {
		$test_type = $this->get_test_type($format);
		if(isset($this->test_type[$test_type])) {
			$tests = $this->test_type[$test_type];
			return $tests;
		} else {
			throw new Validation_Data_Exception (Validation_Data_Exception::ERROR_FORMAT_NOT_DEFINED);
		}
	}
	
	protected function get_tests_values($format) {
		if(isset($this->formats[$format][1])) {
			return $this->formats[$format][1];
		} else {
			throw new Validation_Data_Exception (Validation_Data_Exception::ERROR_FORMAT_NOT_DEFINED);
		}
	}

	function valid_test($test_type,$test_values,$value) {
		switch($test_type) {
			case "mandatory":
				$return = $this->valid_mandatory($test_values,$value);
			break;
			case "regex":
				$return = $this->valid_regex($test_values,$value);
			break;
			case "limit":
				$return = $this->valid_limit($test_values,$value);
			break;
		}
		return $return;
	}
	
	
	private function valid_regex($type,$value) {
		$return = false;
		switch($type) {
			case "Alphanum":
				$return = preg_match("/^[^<>\\=]+$/", $value);
				if($return == false) {
					array_push($this->errors, "format.Alphanum.error");
				}
			break;
			
			case "integer":
				//[-]12456789
				$return = preg_match("/^[\-]?[0-9]+$/", $value);
				if($return == false) {
					array_push($this->errors, "format.integer.error");
				}
			break;
			
			case "float":
				$return = preg_match("/^[\-|\+]?[0-9]+[\.]?[0-9]*?$/", $value);
				if($return == false) {
					array_push($this->errors, "format.float.error");
				}
			break;
			
			case "date_ch":
				//jj.mm.aaaa
				$return = preg_match("/^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}$/", $value);
				if($return) {
					$return = checkdate(substr($value,3,2),substr($value,0,2),substr($value,6,4));
				}
				if($return == false) {
					array_push($this->errors, "format.date_ch.error");
				}
			break;
			
			case "email":
				//$return =  preg_match("/^[a-zA-Z0-9|\-|\.|\_]+[@][a-zA-Z0-9|\-|\.\_]+[\.]{1}[a-zA-Z]{2,}$/", $value);
				$return = filter_var($value, FILTER_VALIDATE_EMAIL);
				if($return == false) {
					array_push($this->errors, "format.email.error");
				}
			break;
			default:
				throw new Validation_Data_Exception (Validation_Data_Exception::ERROR_REGEX_TYPE_NOT_DEFINED);
			break;
		}
		return array("return"=>$return,"break"=>!$return);
	}
	
	private function valid_limit($limit,$value) {
		$limit_type 					= $limit[0];
		$limit_min 						= $limit[1][0];
		$limit_min_inclusive 	= $limit[1][1];
		$limit_max 						= $limit[1][2];
		$limit_max_inclusive	= $limit[1][3];
		if(!($limit_min > $limit_max && $limit_min != false && $limit_max != false)) {
			switch($limit_type) {
				/* first arg must a valid type of field to continue validation */
				case "string":
					$return = $this->valid_limit_string($value,$limit_min,$limit_min_inclusive,$limit_max,$limit_max_inclusive);
				break;
				case "num":
					$return = $this->valid_limit_num($value,$limit_min,$limit_min_inclusive,$limit_max,$limit_max_inclusive);
				break;
				case "date_ch":
					$date = substr($value,6,4).substr($value,3,2).substr($value,0,2);
					$return = $this->valid_limit_num($date,$limit_min,$limit_min_inclusive,$limit_max,$limit_max_inclusive);
				break;
				default:
					throw new Validation_Data_Exception (Validation_Data_Exception::ERROR_LIMIT_TYPE_NOT_DEFINED);
				break;
			}
			if(!$return) {
				array_push($this->errors, "limit.error");
			}
			return array("return"=>$return,"break"=>false);
		} else {
			throw new Validation_Data_Exception (Validation_Data_Exception::ERROR_LIMIT_MIN_MAX);
		}
	}
	
	function valid_limit_string($value,$value_min,$value_min_inclusive,$value_max,$value_max_inclusive) {
		$result = $this->valid_limit_num(strlen($value),$value_min,$value_min_inclusive,$value_max,$value_max_inclusive);
		return $result;
	}
	
	function valid_limit_num($value,$value_min,$value_min_inclusive,$value_max,$value_max_inclusive) {
		$result = false;
		$bitmask =	($value_min !== false)
					+ (($value_min_inclusive !== false) * 2)
					+ (($value_max !== false) * 4)
					+ (($value_max_inclusive !== false) * 8);
		switch($bitmask) {
			// check min value only
			case 1:
				$result = ($value > $value_min);
			break;
			// check min or equal value only
			case 3:
				$result = ($value >= $value_min);
			break;
			// check max value only
			case 4:
				$result = ($value < $value_max);
			break;
			// check min and max value only
			case 5:
				$result = ($value > $value_min) & ($value < $value_max);
			break;
			// check max or equal value only
			case 12:
				$result = ($value <= $value_max);
			break;
			// check min or equal and max value
			case 7:
				$result = ($value >= $value_min) & ($value < $value_max);
			break;
			// check min and max or equal only
			case 13:
				$result = ($value > $value_min) & ($value <= $value_max);
			break;
			// check min or equal and max or equal value
			case 15:
				$result = ($value >= $value_min) & ($value <= $value_max);
			break;
	
		}	// end switch
		return $result;
	}
	
	
	function valid_mandatory($test_value, $value) {
		$return = array("return"=>false,"break"=>false);
		//if (is mandatory)
		if($test_value) {
			if($value == "") {
				array_push($this->errors, "mandatory.error");
				$return = array("return"=>false,"break"=>true);
			} else {
				$return = array("return"=>true,"break"=>false);
			}
		}//if isn t mandatory
		else {
			if($value == "") {
				$return = array("return"=>true,"break"=>true);
			} else {
				$return = array("return"=>true,"break"=>false);
			}
		}
		return $return;
	}
}

?>

<?
//----------------------------------- sous class ------------------------------

Class EB_Validation_Data extends Validation_Data
{
	private $local_formats = array(
		"text1" 		=>array("textfield1",array(true,"Alphanum",array("string",array(3,false,10,false)))),
		"text2" 		=>array("textfield1",array(true,"Alphanum",array("string",array(3,true,10,false)))),
		"text3" 		=>array("textfield1",array(true,"Alphanum",array("string",array(3,false,10,true)))),
		"text4" 		=>array("textfield1",array(false,"Alphanum",array("string",array(3,true,10,true)))),
		"text5" 		=>array("textfield1",array(false,"Alphanum",array("string",array(false,false,10,false)))),
		"text6" 		=>array("textfield1",array(false,"Alphanum",array("string",array(3,false,false,false)))),
		"text7" 		=>array("textfield1",array(false,"Alphanum",array("string",array(3,true,false,false)))),
		"text8" 		=>array("textfield1",array(false,"Alphanum",array("string",array(false,false,false,false)))),
		"integer1" 	=>array("textfield1",array(true,"integer",array("num",array(0,false,10,false)))),
		"integer2" 	=>array("textfield1",array(true,"integer",array("num",array(0,true,10,false)))),
		"integer3" 	=>array("textfield1",array(true,"integer",array("num",array(-5,false,10,false)))),
		"integer4" 	=>array("textfield1",array(true,"integer",array("num",array(-5,true,10,true)))),
		"integer5" 	=>array("textfield1",array(false,"integer",array("num",array(-10,true,-5,true)))),
		"float1" 		=>array("textfield1",array(true,"float",array("num",array(0,false,10,false)))),
		"float2" 		=>array("textfield1",array(true,"float",array("num",array(0,true,10,false)))),
		"float3" 		=>array("textfield1",array(true,"float",array("num",array(-5.2,false,10,false)))),
		"float4" 		=>array("textfield1",array(true,"float",array("num",array(-5.2,true,10.5,true)))),
		"float5" 		=>array("textfield1",array(false,"float",array("num",array(-5.2,true,10.5,true)))),
		"email1" 		=>array("textfield1",array(true,"email",array("string",array(3,false,20,false)))),
		"email2" 		=>array("textfield1",array(true,"email",array("string",array(3,true,10,true)))),
		"email3" 		=>array("textfield1",array(false,"email",array("string",array(3,true,10,true)))),
		"datech1" 	=>array("textfield1",array(true,"date_ch",array("date_ch",array(20060101,true,20061231,true)))),
		"datech2" 	=>array("textfield1",array(true,"date_ch",array("date_ch",array(20060101,true,20061231,true)))),
		"datech3" 	=>array("textfield1",array(false,"date_ch",array("date_ch",array(20060101,true,20061231,false)))));
	
	public function __construct () {
		parent::__construct($this->local_formats);
	}
	
	public function display_errors() {
		foreach($this->errors as $error) {
			//echo "<br />".$error;
			echo " (".$error.")";
		}
	}
	
	public function empty_errors() {
		$this->errors = array();
	}
	
	public function display_format($format) {
		echo "<br />".$this->get_test_type($format);
		$test_values = $this->get_tests_values($format);
		echo "  ----> ";
		if($test_values[0]){echo "obligatoire";}else{echo "facultatif";}
		echo ", ".$test_values[1]." , ";
		$limit = $test_values[2];
		if($limit[1][0] === false && $limit[1][2]== false) {echo "  pas de limit";}
		else {
			if($limit[1][0] !== false) {
				echo $limit[1][0]." <";
				if($limit[1][1]) {
				echo "=";
				}
			}
			echo " valeur ";
			if($limit[1][2] !== false) {
				echo "<";
				if($limit[1][3]) {
					echo "=";
				}
				echo " ".$limit[1][2];
			}
		}
	}
	
}

?>
<?//--------------------------------- fichier de test -------------------

echo "<br>---------------------- Test de la class Validation_data ---------------------";

require("class.Validation_Data.php");
require("class.EB_Validation_Data.php");

$EB_val = new EB_Validation_Data();
function local_test_validation($EB_validation,$type,$value) {
	echo "<br />value : ".$value;
	if($EB_validation->valid_format($type,$value)) {
		echo " --> ok";
	} else {
		echo " -->false";
	}
	$EB_validation->display_errors();
	$EB_validation->empty_errors();
}

echo "<br />textfield => mandatory,regex,limit";
echo "<br />------------------------------- text1 --------------------------";
$EB_val->display_format("text1");
local_test_validation($EB_val,"text1","");
local_test_validation($EB_val,"text1","ab");
local_test_validation($EB_val,"text1","abc");
local_test_validation($EB_val,"text1","abcdefghi");
local_test_validation($EB_val,"text1","abcdefghij");
local_test_validation($EB_val,"text1","abcdefghijk");
local_test_validation($EB_val,"text1","123456789");
local_test_validation($EB_val,"text1","1234567891");
local_test_validation($EB_val,"text1","12345678912");
local_test_validation($EB_val,"text1","éèà");
local_test_validation($EB_val,"text1","ÀÁÂÃÄ");
local_test_validation($EB_val,"text1","ÅÆÇÈÉ");
local_test_validation($EB_val,"text1","ÊËÌÍ");
local_test_validation($EB_val,"text1","ÎÏÐÑÒ");

local_test_validation($EB_val,"text1","ÓÔÕÖØ");
local_test_validation($EB_val,"text1","ÙÚÛÜÝ");
local_test_validation($EB_val,"text1","Þßàáâ");
local_test_validation($EB_val,"text1","ãäåæç");
local_test_validation($EB_val,"text1","èéêëì");
local_test_validation($EB_val,"text1","íîïðñ");
local_test_validation($EB_val,"text1","òóôõöøùú");
local_test_validation($EB_val,"text1","úûýýþÿ");
echo "<br />------------------------------- text2 --------------------------";
$EB_val->display_format("text2");
local_test_validation($EB_val,"text2","");
local_test_validation($EB_val,"text2","ab");
local_test_validation($EB_val,"text2","abc");
local_test_validation($EB_val,"text2","abcdefghi");
local_test_validation($EB_val,"text2","abcdefghij");
local_test_validation($EB_val,"text2","abcdefghijk");
local_test_validation($EB_val,"text2","123456789");
local_test_validation($EB_val,"text2","1234567891");
local_test_validation($EB_val,"text2","12345678912");
local_test_validation($EB_val,"text2","éèà");
echo "<br />------------------------------- text3 --------------------------";
$EB_val->display_format("text3");
local_test_validation($EB_val,"text3","");
local_test_validation($EB_val,"text3","ab");
local_test_validation($EB_val,"text3","abc");
local_test_validation($EB_val,"text3","abcdefghi");
local_test_validation($EB_val,"text3","abcdefghij");
local_test_validation($EB_val,"text3","abcdefghijk");
local_test_validation($EB_val,"text3","123456789");
local_test_validation($EB_val,"text3","1234567891");
local_test_validation($EB_val,"text3","12345678912");
local_test_validation($EB_val,"text3","éèà");
echo "<br />------------------------------- text4 --------------------------";
$EB_val->display_format("text4");
local_test_validation($EB_val,"text4","");
local_test_validation($EB_val,"text4","ab");
local_test_validation($EB_val,"text4","abc");
local_test_validation($EB_val,"text4","abcdefghi");
local_test_validation($EB_val,"text4","abcdefghij");
local_test_validation($EB_val,"text4","abcdefghijk");
local_test_validation($EB_val,"text4","123456789");
local_test_validation($EB_val,"text4","1234567891");
local_test_validation($EB_val,"text4","12345678912");
local_test_validation($EB_val,"text4","éèà");
echo "<br />------------------------------- text5 --------------------------";
$EB_val->display_format("text5");
local_test_validation($EB_val,"text5","");
local_test_validation($EB_val,"text5","ab");
local_test_validation($EB_val,"text5","abc");
local_test_validation($EB_val,"text5","abcdefghi");
local_test_validation($EB_val,"text5","abcdefghij");
local_test_validation($EB_val,"text5","abcdefghijk");
local_test_validation($EB_val,"text5","123456789");
local_test_validation($EB_val,"text5","1234567891");
local_test_validation($EB_val,"text5","12345678912");
local_test_validation($EB_val,"text5","éèà");
echo "<br />------------------------------- text6 --------------------------";
$EB_val->display_format("text6");
local_test_validation($EB_val,"text6","");
local_test_validation($EB_val,"text6","ab");
local_test_validation($EB_val,"text6","abc");
local_test_validation($EB_val,"text6","abcdefghi");
local_test_validation($EB_val,"text6","abcdefghij");
local_test_validation($EB_val,"text6","abcdefghijk");
local_test_validation($EB_val,"text6","123456789");
local_test_validation($EB_val,"text6","1234567891");
local_test_validation($EB_val,"text6","12345678912");
local_test_validation($EB_val,"text6","éèà");
local_test_validation($EB_val,"text6","abc");
local_test_validation($EB_val,"text6","abc");
echo "<br />------------------------------- text7 --------------------------";
$EB_val->display_format("text7");
local_test_validation($EB_val,"text7","");
local_test_validation($EB_val,"text7","ab");
local_test_validation($EB_val,"text7","abc");
local_test_validation($EB_val,"text7","abcdefghi");
local_test_validation($EB_val,"text7","abcdefghij");
local_test_validation($EB_val,"text7","abcdefghijk");
local_test_validation($EB_val,"text7","123456789");
local_test_validation($EB_val,"text7","1234567891");
local_test_validation($EB_val,"text7","12345678912");
local_test_validation($EB_val,"text7","éèà");
local_test_validation($EB_val,"text7","abc");
local_test_validation($EB_val,"text7","abc");
echo "<br />------------------------------- text8 --------------------------";
$EB_val->display_format("text8");
local_test_validation($EB_val,"text8","");
local_test_validation($EB_val,"text8","ab");
local_test_validation($EB_val,"text8","abc");
local_test_validation($EB_val,"text8","abcdefghi");
local_test_validation($EB_val,"text8","abcdefghij");
local_test_validation($EB_val,"text8","abcdefghijk");
local_test_validation($EB_val,"text8","123456789");
local_test_validation($EB_val,"text8","1234567891");
local_test_validation($EB_val,"text8","12345678912");
local_test_validation($EB_val,"text8","éèà");
local_test_validation($EB_val,"text8","abc");
local_test_validation($EB_val,"text8","abc");
local_test_validation($EB_val,"text8","abc");

echo "<br />------------------------------- integer1 --------------------------";
$EB_val->display_format("integer1");
local_test_validation($EB_val,"integer1","");
local_test_validation($EB_val,"integer1","0");
local_test_validation($EB_val,"integer1","1");
local_test_validation($EB_val,"integer1","9");
local_test_validation($EB_val,"integer1","10");
local_test_validation($EB_val,"integer1","-1");
local_test_validation($EB_val,"integer1","11");
local_test_validation($EB_val,"integer1","+5");
local_test_validation($EB_val,"integer1",+5);
local_test_validation($EB_val,"integer1",-3);
local_test_validation($EB_val,"integer1",3.23);
local_test_validation($EB_val,"integer1","-10");
local_test_validation($EB_val,"integer1","10'000");
local_test_validation($EB_val,"integer1","10'00");
local_test_validation($EB_val,"integer1","10.000");
local_test_validation($EB_val,"integer1","10,000");
local_test_validation($EB_val,"integer1","10.000");
local_test_validation($EB_val,"integer1","10 000");
local_test_validation($EB_val,"integer1","10^5");
echo "<br />------------------------------- integer2 --------------------------";
$EB_val->display_format("integer2");
local_test_validation($EB_val,"integer2","");
local_test_validation($EB_val,"integer2","0");
local_test_validation($EB_val,"integer2","1");
local_test_validation($EB_val,"integer2","9");
local_test_validation($EB_val,"integer2","10");
local_test_validation($EB_val,"integer2","-1");
local_test_validation($EB_val,"integer2","11");
local_test_validation($EB_val,"integer2","+5");
local_test_validation($EB_val,"integer2",+5);
local_test_validation($EB_val,"integer2",-3);
local_test_validation($EB_val,"integer2",3.23);
local_test_validation($EB_val,"integer2","-10");
local_test_validation($EB_val,"integer2","10'000");
local_test_validation($EB_val,"integer2","10'00");
local_test_validation($EB_val,"integer2","10.000");
local_test_validation($EB_val,"integer2","10,000");
local_test_validation($EB_val,"integer2","10.000");
local_test_validation($EB_val,"integer2","10 000");
local_test_validation($EB_val,"integer2","10^5");
echo "<br />------------------------------- integer3 --------------------------";
$EB_val->display_format("integer3");
local_test_validation($EB_val,"integer3","");
local_test_validation($EB_val,"integer3","0");
local_test_validation($EB_val,"integer3","1");
local_test_validation($EB_val,"integer3","9");
local_test_validation($EB_val,"integer3","10");
local_test_validation($EB_val,"integer3","-1");
local_test_validation($EB_val,"integer3","11");
local_test_validation($EB_val,"integer3","+5");
local_test_validation($EB_val,"integer3",+5);
local_test_validation($EB_val,"integer3",-3);
local_test_validation($EB_val,"integer3",3.23);
local_test_validation($EB_val,"integer3","-10");
local_test_validation($EB_val,"integer3","10'000");
local_test_validation($EB_val,"integer3","10'00");
local_test_validation($EB_val,"integer3","10.000");
local_test_validation($EB_val,"integer3","10,000");
local_test_validation($EB_val,"integer3","10.000");
local_test_validation($EB_val,"integer3","10 000");
local_test_validation($EB_val,"integer3","10^5");
echo "<br />------------------------------- integer4 --------------------------";
$EB_val->display_format("integer4");
local_test_validation($EB_val,"integer4","");
local_test_validation($EB_val,"integer4","0");
local_test_validation($EB_val,"integer4","1");
local_test_validation($EB_val,"integer4","9");
local_test_validation($EB_val,"integer4","10");
local_test_validation($EB_val,"integer4","-1");
local_test_validation($EB_val,"integer4","11");
local_test_validation($EB_val,"integer4","+5");
local_test_validation($EB_val,"integer4",+5);
local_test_validation($EB_val,"integer4",-3);
local_test_validation($EB_val,"integer4",3.23);
local_test_validation($EB_val,"integer4","-10");
local_test_validation($EB_val,"integer4","10'000");
local_test_validation($EB_val,"integer4","10'00");
local_test_validation($EB_val,"integer4","10.000");
local_test_validation($EB_val,"integer4","10,000");
local_test_validation($EB_val,"integer4","10.000");
local_test_validation($EB_val,"integer4","10 000");
local_test_validation($EB_val,"integer4","10^5");
echo "<br />------------------------------- integer5 --------------------------";
$EB_val->display_format("integer5");
local_test_validation($EB_val,"integer5","");
local_test_validation($EB_val,"integer5","0");
local_test_validation($EB_val,"integer5","1");
local_test_validation($EB_val,"integer5","9");
local_test_validation($EB_val,"integer5","10");
local_test_validation($EB_val,"integer5","-1");
local_test_validation($EB_val,"integer5","11");
local_test_validation($EB_val,"integer5","+5");
local_test_validation($EB_val,"integer5",+5);
local_test_validation($EB_val,"integer5",-3);
local_test_validation($EB_val,"integer5",3.23);
local_test_validation($EB_val,"integer5","-10");
local_test_validation($EB_val,"integer5","10'000");
local_test_validation($EB_val,"integer5","10'00");
local_test_validation($EB_val,"integer5","10.000");
local_test_validation($EB_val,"integer5","10,000");
local_test_validation($EB_val,"integer5","10.000");
local_test_validation($EB_val,"integer5","10 000");
local_test_validation($EB_val,"integer5","10^5");

echo "<br />------------------------------- float1 --------------------------";
$EB_val->display_format("float1");
local_test_validation($EB_val,"float1","");
local_test_validation($EB_val,"float1","0");
local_test_validation($EB_val,"float1","1");
local_test_validation($EB_val,"float1","9");
local_test_validation($EB_val,"float1","9.02");
local_test_validation($EB_val,"float1","9,02");
local_test_validation($EB_val,"float1","9.0.0");
local_test_validation($EB_val,"float1","10");
local_test_validation($EB_val,"float1","-1");
local_test_validation($EB_val,"float1","11");
local_test_validation($EB_val,"float1","+5");
local_test_validation($EB_val,"float1",+5);
local_test_validation($EB_val,"float1",-3);
local_test_validation($EB_val,"float1",3.23);
local_test_validation($EB_val,"float1","-10");
local_test_validation($EB_val,"float1","10'000");
local_test_validation($EB_val,"float1","10'00");
local_test_validation($EB_val,"float1","10.000");
local_test_validation($EB_val,"float1","10,000");
local_test_validation($EB_val,"float1","10.000");
local_test_validation($EB_val,"float1","10 000");
local_test_validation($EB_val,"float1","10^5");
echo "<br />------------------------------- email1 --------------------------";
$EB_val->display_format("email1");
local_test_validation($EB_val,"email1","");
local_test_validation($EB_val,"email1","e.e@sdf.gt");
local_test_validation($EB_val,"email1","e_e@frresd.fr");
local_test_validation($EB_val,"email1","e-df@sdfdsf.de");
local_test_validation($EB_val,"email1","sdf@ds.fr");
local_test_validation($EB_val,"email1","dsfsd@.fr");
local_test_validation($EB_val,"email1","erfre@.frf");
local_test_validation($EB_val,"email1","sdff@d..fr");
local_test_validation($EB_val,"email1","ssf..ssd@sdf.fr");
local_test_validation($EB_val,"email1",".sdfd@sdf.fr");
local_test_validation($EB_val,"email1","2dfdsf@sdf.fr");
local_test_validation($EB_val,"email1","-dfsd@df.gt");
local_test_validation($EB_val,"email1","_sdfdsf@sdff.fr");
local_test_validation($EB_val,"email1","dfgfd@df.22");
local_test_validation($EB_val,"email1","sdfdsf@sdf.AA");
local_test_validation($EB_val,"email1","QQQQ@AA.SS");
local_test_validation($EB_val,"email1","e.e@sdf.gtd");
local_test_validation($EB_val,"email1","e.e@sdf.gtv");
local_test_validation($EB_val,"email1","e.e@sdf.g2v");

echo "<br />------------------------------- datech1 --------------------------";
$EB_val->display_format("datech1");
local_test_validation($EB_val,"email1","");
local_test_validation($EB_val,"datech1","01.01.2006");
local_test_validation($EB_val,"datech1","16.07.2006");
local_test_validation($EB_val,"datech1","31.12.2006");
local_test_validation($EB_val,"datech1","00.01.2006");
local_test_validation($EB_val,"datech1","00.12.006");
local_test_validation($EB_val,"datech1","01.13.2006");
local_test_validation($EB_val,"datech1","01.00.2006");
local_test_validation($EB_val,"datech1","30.02.2006");
local_test_validation($EB_val,"datech1","01.01.20060");
local_test_validation($EB_val,"datech1","01/01/2006");
local_test_validation($EB_val,"datech1","01012006");
local_test_validation($EB_val,"datech1","01-01-2006");

//erreurs trouvées : 
// format Alphanum accepté pour "ÅÆÇÈÉ"

//strlen ("ÅÆÇÈÉ") == 10

?>

Conclusion :


Les formats proposés peuvent etre changé ou ajouté a souhait.
D'ailleurs si vous connaissez une regular expression exact pour les adress mail je suis preneur car il y a quelques erreurs dans la mienne.

J attends vos remarques et vos impressions.

N'hesitez pas a me dire comment ameliorer mon code.

Codes Sources

A voir également

Ajouter un commentaire

Commentaires

codefalse
Messages postés
1127
Date d'inscription
mardi 8 janvier 2002
Statut
Modérateur
Dernière intervention
21 avril 2009
1
pour que mb fonctionne, il faut que l'extension soit activée. C'est le cas chez toi ?

strlen("ÓÔÕÖØ") retourne 10, c'est normal. Il faut attendre php6 (normallement php 5.3 devrait faire l'affaire) et le support de l'unicode pour que cela soit correctement pris en compte.

C'est une sorte de bug dû aux problèmes dans les divers langages parlés (à cause des accents). Et je te parle pas du japonais ! :p

J'avais un bon lien sur le sujet, mais je ne le retrouve plus :(

L'idée qu'il faut garder en mémoire, c'est que toutes les fonction str* ne fonctionnent pas avec les chaines "exotiques". il faut passer par leur équivalent avec mb_* (si cette extension est activée !)
whiskycoke
Messages postés
3
Date d'inscription
mardi 16 décembre 2008
Statut
Membre
Dernière intervention
19 janvier 2009

j ai modifié :
- le code pour que l email soit vérifié avec la fonction filter_var()
- le contenu de la méthode valid_limit_string en mettant valid_limit_num(strlen($value)...

par contre pour ctype c est un peu basique.
pour les formats de chiffre un peu spéciaux comme 100'000 ou -5 ou +3 ou
pour les formats de string alphanum qui accepte les . ; ' je crois qu'il faut passer par les regular expression.
Je vais surement revoir mon regular expression pour le type "Alphanum" et préciser ce que j autorise plutôt que de préciser ce que je n'autorise pas.

j ai toujours le probleme :
strlen("ÓÔÕÖØ") me retourne 10
mb_strlen ne marche pas chez moi

si quelqu'un a une idée
whiskycoke
Messages postés
3
Date d'inscription
mardi 16 décembre 2008
Statut
Membre
Dernière intervention
19 janvier 2009

merci pour tout vos conseils je met a jour des que possible
codefalse
Messages postés
1127
Date d'inscription
mardi 8 janvier 2002
Statut
Modérateur
Dernière intervention
21 avril 2009
1
Pour ma part, j'allais aussi parler de filter_var, mais vu que ca déjà été fait, j'ajouterai l'option "ctype" qui te permette de faire divers tests (très utile aussi quand on récupère des _GET/_POST/...) :

http://fr.php.net/ctype
Arto_8000
Messages postés
1044
Date d'inscription
lundi 7 mars 2005
Statut
Membre
Dernière intervention
13 juillet 2010
5
Petite remarque d'optimisation, tes fonctions "valid_limit_num" et "valid_limit_string" font exactement la même chose à l'exception que dans "valid_limit_string" c'est la longueur de la valeur qui est testé. Tu pourrais résumer ta fonction "valid_limit_string" à ceci :

function valid_limit_string ($value,$value_min,$value_min_inclusive,$value_max,$value_max_inclusive) {
return $this->valid_limit_num(strlen($value),$value_min,$value_min_inclusive,$value_max,$value_max_inclusive);
}

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.