Cursussen/Courses Codesnippets     Top 
PHP - Validatie klasse


1. Validatie klasse
In deze klasse worden enkele methodes voorzien om invoer te controleren.
Er wordt een array met foutberichten bijgehouden en met de methode list_errors() kan de lijst getoond worden.
<?php
class validation {
	var $errors; 
	
	function __construct() {
		$this->errors = array();
	}
	function empty_check($input, $message = "") {
		if (empty($input)) {
			$this->errors[] = $message;
			return true; 
		}
		if (strlen(trim($input)) < 1) {
			$this->errors[] = $message;
			return true; 			
		}
		return false;
	}
	function whole_number_check($input, $message = "") {
		$result = preg_match("/^[0-9]+$/", $input );
		if ($result) {
			return true;
		} else { 
			$this->errors[] = $message;
			return false;
		}
	}	
	function decimal_number_check($input, $message = "") {
		$result = preg_match("/^[0-9.]+$/", $input );
		if ($result) {
			if (substr_count($input,".") > 1) {
				$this->errors[] = $message;
				return false;
			} else {
				return true;
			}
		} else { 
			$this->errors[] = $message;
			return false;
		}
	}	
	function text_check($input, $message = "") {
		$result = preg_match("/^[a-zA-Z ]+$/", $input );
		if ($result) {
			return true;
		} else { 
			$this->errors[] = $message;
			return false;
		}
	}	
	function email_check($input, $message = "") {
		$result = preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/", $input );
		if ($result) {
			return true;
		} else {
			$this->errors[] = $message;
			return false; 
		}
	}
	function date_check($input, $message = "") {

		$result = preg_match("/^[0-9]{4}+\-[0-9]{2}+\-[0-9]{2}+$/", $input );
		if ($result) {
			return true;
		} else {
			$this->errors[] = $message;
			return false;
		}
	}
	function errors_found() {
		if (isset($this->errors) && count($this->errors) > 0) {
			return true;
		} else {
			return false;
		}
	}
	function list_errors($delim = '|') {
		if (isset($this->errors) && count($this->errors) > 0) {
			return implode($delim,$this->errors);
		}
	}	
}


2. Testprogramma
Met dit testprogramma kan je de methodes van de validatie klasse controleren.
De foutberichten array wordt op het scherm getoond.
// validation class test examples
echo "<link rel='stylesheet' type='text/css' href='w3.css'>";
$room_number = "101.2";
$type = "";
$price = 45.50;
$customer_number = "L105540";
$email_address = "test.now@com";
$birth_date = "45/12/2020";
// this validation is normally done when processing the results
// from an input form
require('validation.php');
$valid = new validation();
$valid->whole_number_check($room_number,"the roomnumber is not a whole number");
$valid->empty_check($type,"the type is empty");
$valid->decimal_number_check($price,"the price is not a decimal number");
$valid->whole_number_check($customer_number,"the customer number is not a whole number");
$valid->email_check($email_address,"the e-mail address is not valid");
$valid->date_check($birth_date,"the birth date is not valid");
if ($valid->errors_found() === true) {
	$errors = $valid->list_errors();
	echo "<p class='w3-red'>".str_replace("|","<br>",$errors)."</p>";
}