begin process at 2012 05 27 22:14:32
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Class et Objet ( POO )

 > CLASSE FORM, GESTION DE VALIDATION POUR FORMULAIRE

CLASSE FORM, GESTION DE VALIDATION POUR FORMULAIRE


 Information sur la source

Note :
Aucune note
Catégorie :Class et Objet ( POO ) Classé sous :classe form, formulaire, validation, php 5, obligatoire Niveau :Initié Date de création :10/10/2010 Date de mise à jour :29/03/2011 18:09:12 Vu :5 537

Auteur : phpAnonyme

Ecrire un message privé
Site perso
Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

 Description

Une classe simple d'utilisation, qui permet de valider les données d'un formulaire et d'autoriser son envoi.

- Personnalisation des messages pour chaque types d'erreurs détectés.
- Gère les types : entiers, caractères ou selon vos attentes.
- Gère l'upload de fichier : types mimes, extensions, taille fichier, téléchargement.
- Utilisation des méthodes magiques, pour faciliter les accès aux données envoyés.

Source

  • <?php
  • /**
  • * Form class,
  • * Check fields and data validity
  • * @category classes
  • * @author PHPANONYME
  • * @license GNU/LGPL
  • */
  • /* Exception Handling */
  • class Form_Exception extends Exception { }
  • class Form
  • {
  • /** @var array copy of $_POST or $_GET */
  • public $_method;
  • /** @var array copy of $_FILES */
  • public $_file;
  • /** @var array Save all errors of form */
  • public $_controller;
  • public function __construct($method)
  • {
  • $this->_method = array();
  • $this->_controller = array();
  • $this->_file = (!empty($_FILES)) ? $_FILES : '';
  • if($method AND preg_match('/^(post|get)$/i', $method, $out))
  • $this->_method = ($out[1]==strtolower('post')) ? $_POST : $_GET;
  • else
  • throw new Form_Exception('Indicate the method used, with \'post\' or \'get\'');
  • }
  • /**
  • * Check if we can transmit form
  • * @param string $key key value
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function sendForm()
  • {
  • return (!empty($this->_method) AND empty($this->_controller)) ? TRUE : FALSE;
  • }
  • /**
  • * Return errors or an error with key
  • *
  • * @param string $key key value
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function getError($key = FALSE)
  • {
  • if($this->sendForm())
  • return false;
  • if(empty($this->_method))
  • return false;
  • if($key)
  • return ($this->issetError($key)) ? $this->_controller[$key] : FALSE;
  • return implode('<br/>', $this->_controller);
  • }
  • /**
  • * Check if error exist with key
  • * @param string $key key value
  • * @return boolean Validity is not ok
  • */
  • public function issetError($key)
  • {
  • return (isset($this->_controller[$key]))? TRUE : FALSE;
  • }
  • /**
  • * Save error with key
  • * @param string $key key value
  • * @param string $errorMessage message error
  • * @return boolean Validity is not ok
  • */
  • public function setError($key, $errorMessage)
  • {
  • if(!empty($this->_controller[$key]))
  • return false;
  • if(is_array($errorMessage))
  • $errorMessage = implode('<br/>', $errorMessage);
  • return $this->_controller[$key] = strval($errorMessage);
  • }
  • //////////////////////////////////////////////////////
  • // CONTROLLERS METHODS, used to control forms
  • //////////////////////////////////////////////////////
  • /**
  • * Checks if key exists for methods 'post' or 'get'
  • * @param string $key key value
  • * @param string $message message error
  • * @param boolean $returnError indicates the return type, only a boolean or not
  • * @return boolean Validity is not ok
  • */
  • public function issetValue($key, $message=NULL, $returnError=TRUE)
  • {
  • if (is_array($key)) {
  • foreach($key AS $keyof => $value) {
  • if(isset($this->_method[$keyof]) AND empty($this->_method[$keyof]))
  • $this->setError($keyof, $value);
  • }
  • return;
  • }
  • if($returnError)
  • return (isset($this->_method[$key]) AND empty($this->_method[$key])) ? $this->setError($key, $message) : FALSE;
  • return (isset($this->_method[$key]) AND !empty($this->_method[$key])) ? TRUE : FALSE;
  • }
  • /**
  • * Checks if key exists for methods 'files'
  • * @param string $key key value
  • * @param string $message message error
  • * @param boolean $returnError indicates the return type, only a boolean or not
  • * @return boolean Validity is not ok
  • */
  • public function issetFile($key, $message=NULL, $returnError=TRUE)
  • {
  • if (is_array($key)) {
  • foreach($key AS $keyof => $value) {
  • if(isset($this->_file[$keyof]) AND empty($this->_file[$keyof]['name']))
  • $this->setError($keyof, $value);
  • }
  • return;
  • }
  • if($returnError)
  • return (isset($this->_file[$key]) AND empty($this->_file[$key]['name'])) ? $this->setError($key, $message) : FALSE;
  • return (isset($this->_file[$key]) AND !empty($this->_file[$key]['name'])) ? TRUE : FALSE;
  • }
  • /**
  • * Checks that these are only integers
  • * @param string $key key value
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function isInt($key, $message=NULL)
  • {
  • if (is_array($key)) {
  • foreach($key AS $keyof => $value) {
  • if(isset($this->_method[$keyof]) AND preg_match('/[^0-9]/', $this->_method[$keyof]))
  • $this->setError($keyof, $value);
  • }
  • return;
  • }
  • return (isset($this->_method[$key]) AND preg_match('/[^0-9]/', $this->_method[$key])) ? $this->setError($key, $message) : FALSE;
  • }
  • /**
  • * Checks that these are only characters
  • * @param string $key key value
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function isChar($key, $message = NULL)
  • {
  • if (is_array($key)) {
  • foreach($key AS $keyof => $value) {
  • if(isset($this->_method[$keyof]) AND preg_match('/[^a-z]/i', $this->_method[$keyof]))
  • $this->setError($keyof, $value);
  • }
  • return;
  • }
  • return (isset($this->_method[$key]) AND preg_match('/[^a-z]/i', $this->_method[$key])) ? $this->setError($key, $message) : FALSE;
  • }
  • /**
  • * Checks validity for email
  • * @param string $key key value
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function isEmail($key, $message=NULL)
  • {
  • $pattern = '/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/';
  • if (is_array($key)) {
  • foreach($key AS $keyof => $value) {
  • if(isset($this->_method[$keyof]) AND !preg_match($pattern, $this->_method[$keyof]))
  • $this->setError($keyof, $value);
  • }
  • return;
  • }
  • return (isset($this->_method[$key]) AND !preg_match($pattern, $this->_method[$key])) ? $this->setError($key, $message) : FALSE;
  • }
  • /**
  • * Checks the validity with a custom type
  • * @param string $key key value
  • * @param string $rules pattern customize
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function isExtendType($key, $rules, $message=NULL)
  • {
  • if (is_array($key)) {
  • foreach($key AS $keyof => $value) {
  • if(isset($this->_method[$keyof]) AND preg_match($rules, $this->_method[$keyof]))
  • $this->setError($keyof, $value);
  • }
  • return;
  • }
  • return (isset($this->_method[$key]) AND preg_match($rules, $this->_method[$key])) ? $this->setError($key, $message) : FALSE;
  • }
  • public function isNotExtendType($key, $rules, $message=NULL)
  • {
  • if (is_array($key)) {
  • foreach($key AS $keyof => $value) {
  • if(isset($this->_method[$keyof]) AND !preg_match($rules, $this->_method[$keyof]))
  • $this->setError($keyof, $value);
  • }
  • return;
  • }
  • return (isset($this->_method[$key]) AND !preg_match($rules, $this->_method[$key])) ? $this->setError($key, $message) : FALSE;
  • }
  • /**
  • * Verifies that a file has been transmitted
  • * @param string $key key value
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function isFileTransmit($key, $message=NULL)
  • {
  • if (is_array($key))
  • throw new Form_Exception('Do not use tables');
  • if(empty($this->_file[$key]))
  • return false;
  • $value = $this->issetFile($key, '', FALSE);
  • return ($value AND ($this->_file[$key]['error']!=UPLOAD_ERR_OK OR !is_uploaded_file($this->_file[$key]['tmp_name'])) ) ? $this->setError($key, $message) : FALSE;
  • }
  • /**
  • * Checks the validity of the extension for file
  • * @param string $key key value
  • * @param string or array $extensions extension file
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function isFileExtensions($key, $extensions, $message=NULL)
  • {
  • if (is_array($key))
  • throw new Form_Exception('Do not use tables');
  • if(is_array($extensions))
  • $extensions = implode('|', $extensions);
  • $value = $this->issetFile($key, '', FALSE);
  • return ($value AND !preg_match('/('.$extensions.')$/i', $this->_file[$key]['name'])) ? $this->setError($key, $message) : FALSE;
  • }
  • /**
  • * Checks the validity of the mime type for file
  • * @param string $key key value
  • * @param string or array $mimes mime type
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function isFileMimes($key, $mimes, $message=NULL)
  • {
  • if (is_array($key))
  • throw new Form_Exception('Do not use tables');
  • if(is_array($mimes))
  • $mimes = implode('|', $mimes);
  • $value = $this->issetFile($key, '', FALSE);
  • if(version_compare(substr(phpversion(), 0, 3), '5.3.0') >= 0 OR !class_exists('finfo', false))
  • return ($value AND !preg_match('#^('.$mimes.')$#i', $this->_file[$key]['type'])) ? $this->setError($key, $message) : FALSE;
  • elseif($value) {
  • $finfo = new finfo;
  • $realmime = $finfo->file($this->_file[$key]['tmp_name'], FILEINFO_MIME_TYPE);
  • return (!preg_match('#^('.$mimes.')$#i', $realmime)) ? $this->setError($key, $message) : FALSE;
  • }
  • return false;
  • }
  • /**
  • * Checks the validity of the size for file
  • * @param string $key key value
  • * @param int $size size file in octet
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function isFileSize($key, $size, $message=NULL)
  • {
  • if (is_array($key) OR is_array($size))
  • throw new Form_Exception('Do not use tables');
  • if(preg_match('/[^0-9]/', $size))
  • throw new Form_Exception('Use only int values');
  • $value = $this->issetFile($key, '', FALSE);
  • return ($value AND $size<=$this->_file[$key]['size']) ? $this->setError($key, $message) : FALSE;
  • }
  • /**
  • * Checks the validity of comparing values
  • * @param string $key key value
  • * @param string $message message error
  • * @return boolean Validity is not ok
  • */
  • public function compareValue($key, $value1, $value2, $sign=FALSE, $message=NULL)
  • {
  • $issetKey = $this->issetValue($key, '', FALSE);
  • if(is_array($key))
  • throw new Form_Exception('Do not use tables');
  • if(is_array($value1) AND is_array($value2))
  • throw new Form_Exception('Only one of the two values may be in the form of a table');
  • if(!isset($value1) AND !isset($value2))
  • throw new Form_Exception('Indicate two values to compare');
  • if($sign AND !preg_match('/^(===|==|<=|>=|<>|!=|!==|<|>)$/', $sign))
  • throw new Form_Exception('Specify valid comparison operators');
  • // Operating
  • if(is_array($value1) AND !is_array($value2)){
  • $value1 = implode('|', $value1);
  • return ($issetKey AND preg_match('/^('.$value1.')$/', $value2)) ? FALSE : $this->setError($key, $message);
  • }
  • elseif(!is_array($value1) AND is_array($value2)){
  • $value2 = implode('|', $value2);
  • return ($issetKey AND preg_match('/^('.$value2.')$/', $value1)) ? FALSE : $this->setError($key, $message);
  • }
  • elseif(empty($sign)) {
  • return ($issetKey AND preg_match('/^('.$value1.')$/', $value2)) ? FALSE : $this->setError($key, $message);
  • }
  • else {
  • $comparate = Form::getComparison($value1, $value2, $sign);
  • return ($issetKey AND !$comparate) ? FALSE : $this->setError($key, $message);
  • }
  • }
  • //////////////////////////////////////////////////////
  • // MAGICS METHODS
  • //////////////////////////////////////////////////////
  • public function __get($key)
  • {
  • if(isset($this->_method[$key]))
  • return $this->_method[$key];
  • elseif(isset($this->_file[$key]))
  • return $this->_file[$key]['name'];
  • else
  • return false;
  • }
  • public function __isset($key)
  • {
  • if(isset($this->_method[$key]))
  • return isset($this->_method[$key]);
  • elseif(isset($this->_file[$key]))
  • return isset($this->_file[$key]);
  • }
  • public function __unset($key)
  • {
  • if(isset($this->_method[$key]))
  • unset($this->_method[$key]);
  • if(isset($this->_file[$key]))
  • unset($this->_file[$key]);
  • }
  • //////////////////////////////////////////////////////
  • // STATICS METHODS
  • //////////////////////////////////////////////////////
  • public static function getComparison($value1, $value2, $sign)
  • {
  • switch($sign) {
  • case '===' : return ($value1===$value2) ? TRUE : FALSE;
  • break;
  • case '!==' : return ($value1!==$value2) ? TRUE : FALSE;
  • break;
  • case '==' : return ($value1==$value2) ? TRUE : FALSE;
  • break;
  • case '<=' : return ($value1<=$value2) ? TRUE : FALSE;
  • break;
  • case '>=' : return ($value1>=$value2) ? TRUE : FALSE;
  • break;
  • case '<>' :
  • case '!=' : return ($value1!=$value2) ? TRUE : FALSE;
  • break;
  • case '<' : return ($value1<$value2) ? TRUE : FALSE;
  • break;
  • case '>' : return ($value1>$value2) ? TRUE : FALSE;
  • break;
  • }
  • }
  • }
  • ?>
<?php   
 
/**
  * Form class, 
  * Check fields and data validity
  * @category classes
  * @author PHPANONYME 
  * @license GNU/LGPL
  */
 
  /* Exception Handling */ 
  class Form_Exception extends Exception {  } 
 
class Form
{
	/** @var array copy of $_POST or $_GET */
	public $_method;
	
	/** @var array copy of $_FILES */
	public $_file;
	
	/** @var array Save all errors of form */
	public $_controller;
	
	
	public function __construct($method)
	{
		$this->_method = array();
		$this->_controller = array();
		$this->_file = (!empty($_FILES)) ? $_FILES : '';
		
		if($method AND preg_match('/^(post|get)$/i', $method, $out))
			$this->_method = ($out[1]==strtolower('post')) ?  $_POST : $_GET;
		else
			throw new Form_Exception('Indicate the method used, with \'post\' or \'get\'');	
	}
	
	/**
	* Check if we can transmit form
	* @param string $key key value
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function sendForm()
	{ 
		return (!empty($this->_method) AND empty($this->_controller)) ? TRUE : FALSE;
	}
	
	/**
	* Return errors or an error with key
	*
	* @param string $key key value
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function getError($key = FALSE)
	{
		if($this->sendForm())
			return false;
		if(empty($this->_method))
			return false;
		if($key)
			return ($this->issetError($key)) ? $this->_controller[$key] : FALSE; 
			
		return implode('<br/>', $this->_controller);
	}
	
	/**
	* Check if error exist with key
	* @param string $key key value
	* @return boolean Validity is not ok
	*/
	public function issetError($key)
	{
		return (isset($this->_controller[$key]))? TRUE : FALSE; 
	}
	
	/**
	* Save error with key
	* @param string $key key value
	* @param string $errorMessage message error
	* @return boolean Validity is not ok
	*/
	public function setError($key, $errorMessage) 
	{
		if(!empty($this->_controller[$key]))
			return false;
		if(is_array($errorMessage))
			$errorMessage = implode('<br/>', $errorMessage);
		return $this->_controller[$key] = strval($errorMessage);
	}
	
	//////////////////////////////////////////////////////
	// CONTROLLERS METHODS, used to control forms
	//////////////////////////////////////////////////////
	
	
	/**
	* Checks if key exists for methods 'post' or 'get'
	* @param string $key key value
	* @param string $message message error
	* @param boolean $returnError indicates the return type, only a boolean or not
	* @return boolean Validity is not ok
	*/
	public function issetValue($key, $message=NULL, $returnError=TRUE)
	{
		if (is_array($key)) {
			foreach($key AS $keyof => $value) {
				if(isset($this->_method[$keyof]) AND empty($this->_method[$keyof])) 
					$this->setError($keyof, $value);
			}		
			return;
		}	
		if($returnError)
			return (isset($this->_method[$key]) AND empty($this->_method[$key])) ? $this->setError($key, $message) : FALSE;
		
		return (isset($this->_method[$key]) AND !empty($this->_method[$key])) ? TRUE : FALSE;
	}
	
	/**
	* Checks if key exists for methods 'files'
	* @param string $key key value
	* @param string $message message error
	* @param boolean $returnError indicates the return type, only a boolean or not
	* @return boolean Validity is not ok
	*/
	public function issetFile($key, $message=NULL, $returnError=TRUE)
	{
		if (is_array($key)) {
			foreach($key AS $keyof => $value) {
				if(isset($this->_file[$keyof]) AND empty($this->_file[$keyof]['name']))
					$this->setError($keyof, $value);
			}		
			return;
		}	
		if($returnError)	
			return (isset($this->_file[$key]) AND empty($this->_file[$key]['name'])) ? $this->setError($key, $message) : FALSE;
		
		return (isset($this->_file[$key]) AND !empty($this->_file[$key]['name'])) ? TRUE : FALSE;	
	}
	
	/**
	* Checks that these are only integers
	* @param string $key key value
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function isInt($key, $message=NULL)
	{
		if (is_array($key)) {
			foreach($key AS $keyof => $value) {
				if(isset($this->_method[$keyof]) AND preg_match('/[^0-9]/', $this->_method[$keyof]))
					$this->setError($keyof, $value);
			}		
			return;
		}
		return (isset($this->_method[$key]) AND preg_match('/[^0-9]/', $this->_method[$key])) ? $this->setError($key, $message) : FALSE;
	}
	
	/**
	* Checks that these are only characters
	* @param string $key key value
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function isChar($key, $message = NULL)
	{
		if (is_array($key)) {
			foreach($key AS $keyof => $value) {
				if(isset($this->_method[$keyof]) AND preg_match('/[^a-z]/i', $this->_method[$keyof]))
					$this->setError($keyof, $value);
			}		
			return;
		}
		return (isset($this->_method[$key]) AND preg_match('/[^a-z]/i', $this->_method[$key])) ? $this->setError($key, $message) : FALSE;
	}
	
	/**
	* Checks validity for email 
	* @param string $key key value
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function isEmail($key, $message=NULL)
	{
		$pattern = '/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/';
		if (is_array($key)) {
			foreach($key AS $keyof => $value) {
				if(isset($this->_method[$keyof]) AND !preg_match($pattern, $this->_method[$keyof]))
					$this->setError($keyof, $value);
			}		
			return;
		}
		return (isset($this->_method[$key]) AND !preg_match($pattern, $this->_method[$key])) ? $this->setError($key, $message) : FALSE;	
	}
	
	/**
	* Checks the validity with a custom type
	* @param string $key key value
	* @param string $rules pattern customize 
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function isExtendType($key, $rules, $message=NULL)
	{
		if (is_array($key)) {
			foreach($key AS $keyof => $value) {
				if(isset($this->_method[$keyof]) AND preg_match($rules, $this->_method[$keyof]))
					$this->setError($keyof, $value);
			}		
			return;
		}
		return (isset($this->_method[$key]) AND preg_match($rules, $this->_method[$key])) ? $this->setError($key, $message) : FALSE;	
	}
	
	public function isNotExtendType($key, $rules, $message=NULL)
	{
		if (is_array($key)) {
			foreach($key AS $keyof => $value) {
				if(isset($this->_method[$keyof]) AND !preg_match($rules, $this->_method[$keyof]))
					$this->setError($keyof, $value);
			}		
			return;
		}
		return (isset($this->_method[$key]) AND !preg_match($rules, $this->_method[$key])) ? $this->setError($key, $message) : FALSE;	
	}
	
	/**
	* Verifies that a file has been transmitted
	* @param string $key key value
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function isFileTransmit($key, $message=NULL)
	{
		if (is_array($key))
			throw new Form_Exception('Do not use tables');
		if(empty($this->_file[$key]))
			return false;
			
		$value = $this->issetFile($key, '', FALSE);		
		return	($value AND ($this->_file[$key]['error']!=UPLOAD_ERR_OK OR !is_uploaded_file($this->_file[$key]['tmp_name'])) ) ? $this->setError($key, $message) : FALSE;	
	}
	
	/**
	* Checks the validity of the extension for file
	* @param string $key key value
	* @param string or array $extensions extension file
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function isFileExtensions($key, $extensions, $message=NULL)
	{
		if (is_array($key))
			throw new Form_Exception('Do not use tables');
		if(is_array($extensions))
			$extensions = implode('|', $extensions);
			
		$value = $this->issetFile($key, '', FALSE);	
		return ($value AND !preg_match('/('.$extensions.')$/i', $this->_file[$key]['name'])) ? $this->setError($key, $message) : FALSE;
	}
	
	/**
	* Checks the validity of the mime type for file
	* @param string $key key value
	* @param string or array $mimes mime type
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function isFileMimes($key, $mimes, $message=NULL)
	{
		if (is_array($key))
			throw new Form_Exception('Do not use tables');
		if(is_array($mimes))
			$mimes = implode('|', $mimes);
		
		$value = $this->issetFile($key, '', FALSE);		
		if(version_compare(substr(phpversion(), 0, 3), '5.3.0') >= 0 OR !class_exists('finfo', false)) 
			return ($value AND !preg_match('#^('.$mimes.')$#i', $this->_file[$key]['type'])) ? $this->setError($key, $message) : FALSE;
		elseif($value) { 
			$finfo = new finfo;
			$realmime = $finfo->file($this->_file[$key]['tmp_name'], FILEINFO_MIME_TYPE);
			return (!preg_match('#^('.$mimes.')$#i', $realmime)) ? $this->setError($key, $message) : FALSE;
		}
		return false;
	}
	
	/**
	* Checks the validity of the size for file
	* @param string $key key value
	* @param int $size size file in octet
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function isFileSize($key, $size, $message=NULL)
	{
		if (is_array($key) OR is_array($size))
			throw new Form_Exception('Do not use tables');
		if(preg_match('/[^0-9]/', $size))	
			throw new Form_Exception('Use only int values');

		$value = $this->issetFile($key, '', FALSE);
		return ($value AND $size<=$this->_file[$key]['size']) ? $this->setError($key, $message) : FALSE;
	}
	
	/**
	* Checks the validity of comparing values
	* @param string $key key value
	* @param string $message message error
	* @return boolean Validity is not ok
	*/
	public function compareValue($key, $value1, $value2, $sign=FALSE, $message=NULL)
	{
		$issetKey = $this->issetValue($key, '', FALSE);

		if(is_array($key))
			throw new Form_Exception('Do not use tables');
		if(is_array($value1) AND is_array($value2))
			throw new Form_Exception('Only one of the two values may be in the form of a table');
		if(!isset($value1) AND !isset($value2))
			throw new Form_Exception('Indicate two values to compare'); 
		if($sign AND !preg_match('/^(===|==|<=|>=|<>|!=|!==|<|>)$/', $sign))
			throw new Form_Exception('Specify valid comparison operators');
		
		// Operating
		if(is_array($value1) AND !is_array($value2)){
			$value1 = implode('|', $value1);
			return ($issetKey AND preg_match('/^('.$value1.')$/', $value2)) ? FALSE : $this->setError($key, $message);
		}
		elseif(!is_array($value1) AND is_array($value2)){
			$value2 = implode('|', $value2);
			return ($issetKey AND preg_match('/^('.$value2.')$/', $value1)) ? FALSE : $this->setError($key, $message);
		}
		elseif(empty($sign)) {
			return ($issetKey AND preg_match('/^('.$value1.')$/', $value2)) ? FALSE : $this->setError($key, $message);
		}
		else {
			$comparate = Form::getComparison($value1, $value2, $sign);
			return ($issetKey AND !$comparate) ? FALSE : $this->setError($key, $message);
		}
	}
	
	//////////////////////////////////////////////////////
	// MAGICS METHODS 
	//////////////////////////////////////////////////////
	
	public function __get($key)
	{
		if(isset($this->_method[$key])) 
			return $this->_method[$key];
		elseif(isset($this->_file[$key]))
			return $this->_file[$key]['name'];
		else
			return false;
	}
	
	public function __isset($key)
	{
		if(isset($this->_method[$key])) 
			return isset($this->_method[$key]);
		elseif(isset($this->_file[$key]))
			return isset($this->_file[$key]);
	}

	public function __unset($key)
	{
		if(isset($this->_method[$key])) 
			unset($this->_method[$key]);
		if(isset($this->_file[$key]))
			unset($this->_file[$key]);
	}
	
	//////////////////////////////////////////////////////
	// STATICS METHODS 
	//////////////////////////////////////////////////////
	
	public static function getComparison($value1, $value2, $sign)
	{
		switch($sign) {
			case '===' : return ($value1===$value2) ? TRUE : FALSE;
			break;
			case '!==' : return ($value1!==$value2) ? TRUE : FALSE;
			break;
			case '==' : return ($value1==$value2) ? TRUE : FALSE;
			break;
			case '<=' : return ($value1<=$value2) ? TRUE : FALSE;
			break;
			case '>=' : return ($value1>=$value2) ? TRUE : FALSE;
			break;	
			case '<>' :
				case '!=' : return ($value1!=$value2) ? TRUE : FALSE;
			break;
			case '<' : return ($value1<$value2) ? TRUE : FALSE;
			break;	
			case '>' : return ($value1>$value2) ? TRUE : FALSE;
			break;
		}
	}
	
}

?>




 Historique

11 octobre 2010 02:07:45 :
Ajout gestion validation d'upload
11 octobre 2010 02:10:53 :
-------------------
11 octobre 2010 21:52:06 :
-----------------
12 octobre 2010 20:45:37 :
Correction de certaines choses et ajout d'un paramètre
18 octobre 2010 05:59:40 :
Réécriture en partie des classes, de la documentation, et des commentaires
18 octobre 2010 16:46:26 :
---
18 octobre 2010 18:42:17 :
Ouppps !!! correction d'un léger souci d'initialisation de la variable LockForm, qui empêchait la validation du formulaire en cas de succès.
14 décembre 2010 01:18:37 :
Réécriture COMPLETE de la classe, suite à certaines restrictions de l'ancienne version et son manque de permissivité d'évolution.
15 décembre 2010 18:05:47 :
correction bugs
29 mars 2011 16:32:55 :
- Correction de bugs et, correction/changement de certaines variables
29 mars 2011 18:09:12 :
del zip

 Sources du même auteur

CALCULATEUR DE SUREBET POUR PARIS SPORTIF
CRÉATION D'UN SCRIPT D'AUTHENTIFICATION
Source avec Zip [POO] JEU DE KEMO OU DE PENDULE
TESTE DU KHI-DEUX/CHI-DEUX D'INDÉPENDANCE

 Sources de la même categorie

Source avec Zip GÉNÉRATION AUTOMATIQUE DE FICHIER .CLASS.PHP EN FONCTION D'U... par ig3
CLASSE D'OBJET DE CRYPTAGE ET DÉCRYPTAGE DE CHAINES DE CARAC... par 8Tnerolf8
Source avec Zip MY.DEVIANTART API par inwebo
CLASSE DE GESTION DE "VARIABLES GLOBALES D'ENVIRONNEMENT" par pifou25
Source avec Zip COLLECTION.CLASS.MIN.PHP par thunderhunter

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture VALIDATEUR DE FORMULAIRE par Reldan
Source avec Zip FORMULAIRE D'INSCRIPTION par lelioua
FORM, ORM POUR FORMULAIRE par choy
Source avec Zip Source avec une capture FORMOL, PACKAGE PHP5 DE GESTION DE FORMULAIRES XHTML par tmaziere
Source avec Zip [PHP5] FORMCHECKER : VALIDATION DE SAISIES UTILISATEUR par malalam

Commentaires et avis

Commentaire de begueradj le 08/06/2011 17:17:16

cool, je vais tester tout ça avec votre tutorial

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

champs formulaire obligatoire [ par Blacknight91titi ] Salutvoila j'aimerai savoir comment faire pour que dans un formulaire on soit obliger de remplir certain champs, et si un parmis eu n'est pas rempli a Prob de redirection et validation [ par LeManchot ] Voila jai encoren un souci:jai un formulaire que je valide via un bouton (pr linstant ok) mais par ce meme bouton je dois rediriger la page ou se toru Prob Debutant - Validation d'1 formulaire par Menu Déroulant . [ par marcooo ] Bonjour a tous .J'esplique le problème ... Tout d'abord je tien a dire ke je débute dans le PHP/MySQL.Alors voila, je voudrai créer une sorte de moteu Validation Formulaire [ par kiboumz ] Bonjour, J'ai cr&#233;e un formulaire duquel j'envoie des donn&#233;es vers une base de donn&#233;es, cependant j'aimerais pouvoir valider mes donn& Problème de validation de formulaire [ par ehmarc ] Hello world!!!Bon j'ai un petit problème jai formulaire tres simple une case identification un bouton envoyerj'arrive à m'identifier nickel quand quan Double envoie lors d'une validation de formulaire? [ par Monico9385 ] Bonjour &#224; tous, j'ai un petit soucis concernant un double envoie lors de la validation d'un formulaire, je m'explique : j'ai sur un site un for Activation d'un compte par mail [ par aissiou1979 ] Voila.J'ai realisé un formulaire d'inscription pour jouer à un concours.A la suite de la validation de ce formulaire un mail est envoyé à  la personne validation de courriel [ par luciano12 ] Bonjour,J'aurais besoin de votre aide pour m'aider à faire la chose suivante:Je voudrais que les visiteurs qui visitent mon site Intenret puissent rem Balise select liées [ par nagrom_om ] Salut, Voila mon probleme : J'ai une page dans laquelle il y a une balise select qui affiche une liste d'éléments, le choix d un élément provoque l af CLASS PERMETTANT DE CRÉER UNE IMAGE POUR LA VALIDATION D'UN FORMULAIRE [ par Tanaka56 ] Bonjour à tous.<br style="mso


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,577 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales