210 lines
6.7 KiB
PHP
210 lines
6.7 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'];
|
|
$field = $this->validationField['field'];
|
|
|
|
// For Eloquent models, we can call the static method directly
|
|
$value = $model::checkUnique($recordID, $field, $this->fieldValue);
|
|
|
|
if (!$value) $this->errorMessages[] = 'invoer moet uniek zijn';
|
|
}
|
|
|
|
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';
|
|
}
|
|
}
|