From 1881054c92b879aa79b14c49ba98f49df0943078 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 8 May 2025 15:00:43 +0100 Subject: [PATCH] Fixed #16863 - better handle unique not required custom field redirects Signed-off-by: snipe --- app/Http/Controllers/Assets/AssetCheckinController.php | 9 ++++----- .../Controllers/Assets/AssetCheckoutController.php | 10 +++++----- app/Http/Controllers/Assets/AssetsController.php | 10 +++++----- app/Models/CustomFieldset.php | 5 ++++- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php index bc9ae19c8e..083975867b 100644 --- a/app/Http/Controllers/Assets/AssetCheckinController.php +++ b/app/Http/Controllers/Assets/AssetCheckinController.php @@ -42,12 +42,11 @@ class AssetCheckinController extends Controller return redirect()->route('hardware.show', $asset->id)->with('error', trans('admin/hardware/general.model_invalid_fix')); } - // Validate custom fields on existing asset - $validator = Validator::make($asset->toArray(), $asset->customFieldValidationRules()); + // Invoke the validation to see if the audit will complete successfully + $asset->setRules($asset->getRules() + $asset->customFieldValidationRules()); - if ($validator->fails()) { - return redirect()->route('hardware.edit', $asset) - ->withErrors($validator); + if ($asset->isInvalid()) { + return redirect()->route('hardware.edit', $asset)->withErrors($asset->getErrors()); } $target_option = match ($asset->assigned_type) { diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.php index 26a993d808..da2c0a2550 100644 --- a/app/Http/Controllers/Assets/AssetCheckoutController.php +++ b/app/Http/Controllers/Assets/AssetCheckoutController.php @@ -37,13 +37,13 @@ class AssetCheckoutController extends Controller ->with('error', trans('admin/hardware/general.model_invalid_fix')); } - // Validate custom fields on existing asset - $validator = Validator::make($asset->toArray(), $asset->customFieldValidationRules()); + // Invoke the validation to see if the audit will complete successfully + $asset->setRules($asset->getRules() + $asset->customFieldValidationRules()); - if ($validator->fails()) { - return redirect()->route('hardware.edit', $asset) - ->withErrors($validator); + if ($asset->isInvalid()) { + return redirect()->route('hardware.edit', $asset)->withErrors($asset->getErrors()); } + if ($asset->availableForCheckout()) { return view('hardware/checkout', compact('asset')) diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php index c47741b5a3..12cb93ff3e 100755 --- a/app/Http/Controllers/Assets/AssetsController.php +++ b/app/Http/Controllers/Assets/AssetsController.php @@ -882,12 +882,12 @@ class AssetsController extends Controller $this->authorize('audit', Asset::class); $settings = Setting::getSettings(); - // Validate custom fields on existing asset - $validator = Validator::make($asset->toArray(), $asset->customFieldValidationRules()); - if ($validator->fails()) { - return redirect()->route('hardware.edit', $asset) - ->withErrors($validator); + // Invoke the validation to see if the audit will complete successfully + $asset->setRules($asset->getRules() + $asset->customFieldValidationRules()); + + if ($asset->isInvalid()) { + return redirect()->route('hardware.edit', $asset)->withErrors($asset->getErrors()); } $dt = Carbon::now()->addMonths($settings->audit_interval)->toDateString(); diff --git a/app/Models/CustomFieldset.php b/app/Models/CustomFieldset.php index bf04817685..e5490b49b3 100644 --- a/app/Models/CustomFieldset.php +++ b/app/Models/CustomFieldset.php @@ -113,7 +113,10 @@ class CustomFieldset extends Model $rule[] = 'unique_undeleted'; } - array_push($rule, $field->attributes['format']); + if ($field->attributes['format']!='') { + array_push($rule, $field->attributes['format']); + } + $rules[$field->db_column_name()] = $rule;