fix for legacy validation
This commit is contained in:
@@ -75,7 +75,8 @@ class UsersController extends BaseController
|
|||||||
|
|
||||||
$user = $this->request->getPost();
|
$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']);
|
unset($user['id']);
|
||||||
|
|
||||||
if ($userID == 0) {
|
if ($userID == 0) {
|
||||||
|
|||||||
@@ -153,13 +153,41 @@ class Validation
|
|||||||
public function checkUnique(): void
|
public function checkUnique(): void
|
||||||
{
|
{
|
||||||
$recordID = $this->request->getPost('id');
|
$recordID = $this->request->getPost('id');
|
||||||
$model = $this->validationField['model'];
|
$model = $this->validationField['model'] ?? null;
|
||||||
$field = $this->validationField['field'];
|
$field = $this->validationField['field'] ?? null;
|
||||||
|
|
||||||
// For Eloquent models, we can call the static method directly
|
if (!$model || !$field) {
|
||||||
$value = $model::checkUnique($recordID, $field, $this->fieldValue);
|
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
|
public function checkDate(): void
|
||||||
|
|||||||
Reference in New Issue
Block a user