Files
dbois/application/libraries/validation/Validation.php
T
2018-01-10 14:09:34 +01:00

224 lines
6.5 KiB
PHP

<?php defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );
/**
* Created by PhpStorm.
* User: imac-3
* Date: 04/06/16
* Time: 14:17
*/
class validation {
private $fieldValue;
private $validations;
private $validation;
private $validationResult;
private $errorMessages = [];
private $validationField;
private $ci;
public function __construct()
{
$this->ci =& get_instance();
}
public function validate( $fieldValue, $validationField = FALSE )
{
$this->setValidationField( $validationField );
$this->setFieldValue( $fieldValue );
$this->handle();
}
public function setValidationField( $validationfield )
{
$this->validationField = $validationfield;
}
public function getErrorMessages()
{
return $this->errorMessages;
}
public function getValidationResult()
{
return $this->validationResult;
}
private function handle()
{
$this->setValidations();
$this->checkValidations();
$this->setValidationResult();
}
private function setValidations()
{
$this->validations = explode( '|', $this->validationField['validations'] );
}
private function setFieldValue( $fieldValue )
{
$this->fieldValue = $fieldValue;
}
private function setValidationResult()
{
if ( count( $this->errorMessages ) == 0 ) return $this->validationResult = TRUE;
$this->validationResult = FALSE;
}
private function checkValidations()
{
foreach ( $this->validations as $this->validation )
{
if ( $this->validation == 'required' ) $this->checkRequired();
if ( $this->validation == 'alpha' ) $this->checkAlfa();
if ( $this->validation == 'alphaNumeric' ) $this->checkAlfanumeric();
if ( $this->validation == 'alphaNumericSpaces' ) $this->checkAlphaNumericSpaces();
if ( $this->validation == 'alphaDash' ) $this->checkAlphaDash();
if ( $this->validation == 'numeric' ) $this->checkNumeric();
if ( $this->validation == 'integer' ) $this->checkInteger();
if ( $this->validation == 'decimal' ) $this->checkDecimal();
if ( $this->validation == 'email' ) $this->checkEmail();
if ( $this->validation == 'unique' ) $this->checkUnique();
if( $this->validation == 'date' ) $this->checkdate();
if ( substr( $this->validation, 0, 9 ) == 'minlength' ) $this->checkMinlength();
if ( substr( $this->validation, 0, 9 ) == 'maxlength' ) $this->checkMaxlength();
if ( substr( $this->validation, 0, 8 ) == 'minvalue' ) $this->checkMinvalue();
if ( substr( $this->validation, 0, 8 ) == 'maxvalue' ) $this->checkMaxvalue();
}
}
public function checkRequired()
{
if ( ! $this->fieldValue || empty( $this->fieldValue ) || strlen( $this->fieldValue ) == 0 )
$this->errorMessages[] = 'veld is verplicht';
}
public function checkAlfa()
{
$value = ctype_alpha( $this->fieldValue );
if ( ! $value ) $this->errorMessages[] = 'alleen letters toegestaan';
}
public function checkAlfaNumeric()
{
$value = ctype_alnum( (string) $this->fieldValue );
if ( ! $value ) $this->errorMessages[] = 'alleen letters en cijfers toegestaan';
return $value;
}
public function checkAlphaNumericSpaces()
{
$value = (bool) preg_match( '/^[A-Z0-9 ]+$/i', $this->fieldValue );
if ( ! $value ) $this->errorMessages[] = 'alleen letters, cijfers en spaties toegestaan';
}
public function checkAlphaDash()
{
$value = (bool) preg_match( '/^[a-z0-9_-]+$/i', $this->fieldValue );
if ( ! $value ) $this->errorMessages[] = 'alleen letters, cijfers en streepjes toegestaan';
}
public function checkNumeric()
{
$value = (bool) preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $this->fieldValue );
if ( ! $value ) $this->errorMessages[] = 'alleen getallen toegestaan';
}
public function checkInteger()
{
$value = (bool) preg_match( '/^[\-+]?[0-9]+$/', $this->fieldValue );
if ( ! $value ) $this->errorMessages[] = 'alleen gehele getallen toegestaan';
}
public function checkDecimal()
{
$value = (bool) preg_match( '/^[\-+]?[0-9]+\.[0-9]+$/', $this->fieldValue );
if ( ! $value ) $this->errorMessages[] = 'alleen getallen toegestaan';
}
public function checkEmail()
{
if ( ! $this->fieldValue || empty( $this->fieldValue ) || strlen( $this->fieldValue ) == 0 ) return;
$value = (bool) filter_var( $this->fieldValue, FILTER_VALIDATE_EMAIL );
if ( ! $value ) $this->errorMessages[] = 'ongeldig emailadres';
}
public function checkUnique()
{
$recordID = $this->ci->input->post( 'id' );
$model = $this->validationField['model'];
$field = $this->validationField['field'];
$value = $this->ci
->$model
->checkUnique( $recordID, $field, $this->fieldValue );
if ( ! $value ) $this->errorMessages[] = 'invoer moet uniek zijn';
}
public function checkDate()
{
if( empty($this->fieldValue) ) return;
$dateParts = explode('-', $this->fieldValue);
if( count($dateParts) != 3) $dateParts = explode('/', $this->fieldValue);
if( count($dateParts) != 3)
return $this->errorMessages[] = 'invoer is geen datum';
if( ! checkdate($dateParts[1], $dateParts[0], $dateParts[2] ))
$this->errorMessages[] = 'invoer is geen datum';
}
private function checkMinlength()
{
$length = explode( '-', $this->validation );
if ( strlen( $this->fieldValue ) < $length[1] )
$this->errorMessages[] = 'moet minimaal uit ' . $length[1] . ' karakters bestaan';
}
private function checkMaxlength()
{
$length = explode( $this->validation, '-' );
if ( strlen( $this->fieldValue ) > $length[1] )
$this->errorMessages[] = 'mag maximaal uit ' . $length[1] . ' karakters bestaan';
}
private function checkMinValue()
{
$value = explode( $this->validation, '-' );
if( $this->fieldValue < $value[1])
$this->errorMessages[] = 'moet minimaal ' . $value[1] . ' zijn';
}
private function checkMaxValue()
{
$value = explode( $this->validation, '-' );
if( $this->fieldValue > $value[1])
$this->errorMessages[] = 'mag maximaal ' . $value[1] . ' zijn';
}
}