From 506c88a5bd0c4dc1109124165dae962063801209 Mon Sep 17 00:00:00 2001 From: Theo Weijmer Date: Tue, 20 Jan 2026 13:57:03 +0100 Subject: [PATCH] fix for legacy validation --- app/Controllers/Beheer/UsersController.php | 3 +- app/Libraries/Validation/Validation.php | 38 +++++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/app/Controllers/Beheer/UsersController.php b/app/Controllers/Beheer/UsersController.php index 86fcd4c..757669d 100644 --- a/app/Controllers/Beheer/UsersController.php +++ b/app/Controllers/Beheer/UsersController.php @@ -75,7 +75,8 @@ class UsersController extends BaseController $user = $this->request->getPost(); - $userID = $user['id']; + // Ensure we always have an ID, default to 0 for new user + $userID = (int)($user['id'] ?? 0); unset($user['id']); if ($userID == 0) { diff --git a/app/Libraries/Validation/Validation.php b/app/Libraries/Validation/Validation.php index d9c8ab1..85e6907 100644 --- a/app/Libraries/Validation/Validation.php +++ b/app/Libraries/Validation/Validation.php @@ -153,13 +153,41 @@ class Validation public function checkUnique(): void { $recordID = $this->request->getPost('id'); - $model = $this->validationField['model']; - $field = $this->validationField['field']; + $model = $this->validationField['model'] ?? null; + $field = $this->validationField['field'] ?? null; - // For Eloquent models, we can call the static method directly - $value = $model::checkUnique($recordID, $field, $this->fieldValue); + if (!$model || !$field) { + return; // nothing to validate against + } - if (!$value) $this->errorMessages[] = 'invoer moet uniek zijn'; + // 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