Files
2026-01-20 13:57:03 +01:00

238 lines
7.5 KiB
PHP

<?php
namespace App\Libraries\Validation;
use CodeIgniter\HTTP\IncomingRequest;
/**
* Custom Validation Library
* Migrated from CodeIgniter 3 to CodeIgniter 4
*/
class Validation
{
private $fieldValue;
private $validations;
private $validation;
private $validationResult;
private array $errorMessages = [];
private $validationField;
protected IncomingRequest $request;
public function __construct()
{
$this->request = \Config\Services::request();
}
public function validate($fieldValue, $validationField = false): void
{
$this->setValidationField($validationField);
$this->setFieldValue($fieldValue);
$this->handle();
}
public function setValidationField($validationfield): void
{
$this->validationField = $validationfield;
}
public function getErrorMessages(): array
{
return $this->errorMessages;
}
public function getValidationResult(): bool
{
return $this->validationResult;
}
private function handle(): void
{
$this->setValidations();
$this->checkValidations();
$this->setValidationResult();
}
private function setValidations(): void
{
$this->validations = explode('|', $this->validationField['validations']);
}
private function setFieldValue($fieldValue): void
{
$this->fieldValue = $fieldValue;
}
private function setValidationResult(): void
{
if (count($this->errorMessages) == 0) {
$this->validationResult = true;
return;
}
$this->validationResult = false;
}
private function checkValidations(): void
{
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(): void
{
if (!$this->fieldValue || empty($this->fieldValue) || strlen($this->fieldValue) == 0)
$this->errorMessages[] = 'veld is verplicht';
}
public function checkAlfa(): void
{
$value = ctype_alpha($this->fieldValue);
if (!$value) $this->errorMessages[] = 'alleen letters toegestaan';
}
public function checkAlfaNumeric(): bool
{
$value = ctype_alnum((string)$this->fieldValue);
if (!$value) $this->errorMessages[] = 'alleen letters en cijfers toegestaan';
return $value;
}
public function checkAlphaNumericSpaces(): void
{
$value = (bool)preg_match('/^[A-Z0-9 ]+$/i', $this->fieldValue);
if (!$value) $this->errorMessages[] = 'alleen letters, cijfers en spaties toegestaan';
}
public function checkAlphaDash(): void
{
$value = (bool)preg_match('/^[a-z0-9_-]+$/i', $this->fieldValue);
if (!$value) $this->errorMessages[] = 'alleen letters, cijfers en streepjes toegestaan';
}
public function checkNumeric(): void
{
$value = (bool)preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $this->fieldValue);
if (!$value) $this->errorMessages[] = 'alleen getallen toegestaan';
}
public function checkInteger(): void
{
$value = (bool)preg_match('/^[\-+]?[0-9]+$/', $this->fieldValue);
if (!$value) $this->errorMessages[] = 'alleen gehele getallen toegestaan';
}
public function checkDecimal(): void
{
$value = is_numeric($this->fieldValue);
if (!$value) $this->errorMessages[] = 'alleen getallen toegestaan';
}
public function checkEmail(): void
{
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(): void
{
$recordID = $this->request->getPost('id');
$model = $this->validationField['model'] ?? null;
$field = $this->validationField['field'] ?? null;
if (!$model || !$field) {
return; // nothing to validate against
}
// Support legacy CI3 model naming like "User_model"
if (!class_exists($model) && str_ends_with($model, '_model')) {
$model = substr($model, 0, -6); // trim suffix
}
if (!class_exists($model)) {
// Fallback: cannot validate uniqueness without a model; do not hard-fail
return;
}
// Generic Eloquent uniqueness check
try {
$query = $model::where($field, '=', $this->fieldValue);
// ignore current record on update
if (!empty($recordID)) {
$query = $query->where('id', '!=', $recordID);
}
$exists = method_exists($query, 'exists') ? $query->exists() : ($query->count() > 0);
if ($exists) {
$this->errorMessages[] = 'invoer moet uniek zijn';
}
} catch (\Throwable $e) {
// Do not crash on validation; skip unique check if model/field invalid
return;
}
}
public function checkDate(): void
{
if (empty($this->fieldValue)) return;
$dateParts = explode('-', $this->fieldValue);
if (count($dateParts) != 3) $dateParts = explode('/', $this->fieldValue);
if (count($dateParts) != 3) {
$this->errorMessages[] = 'invoer is geen datum';
return;
}
if (!checkdate((int)$dateParts[1], (int)$dateParts[0], (int)$dateParts[2]))
$this->errorMessages[] = 'invoer is geen datum';
}
private function checkMinlength(): void
{
$length = explode('-', $this->validation);
if (strlen($this->fieldValue) < (int)$length[1])
$this->errorMessages[] = 'moet minimaal uit ' . $length[1] . ' karakters bestaan';
}
private function checkMaxlength(): void
{
$length = explode('-', $this->validation);
if (strlen($this->fieldValue) > (int)$length[1])
$this->errorMessages[] = 'mag maximaal uit ' . $length[1] . ' karakters bestaan';
}
private function checkMinValue(): void
{
$value = explode('-', $this->validation);
if ($this->fieldValue < (float)$value[1])
$this->errorMessages[] = 'moet minimaal ' . $value[1] . ' zijn';
}
private function checkMaxValue(): void
{
$value = explode('-', $this->validation);
if ($this->fieldValue > (float)$value[1])
$this->errorMessages[] = 'mag maximaal ' . $value[1] . ' zijn';
}
}