From 6d65f6646f43d9ceb2f84ed7df0a00e707657841 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 14 Nov 2023 14:55:51 -0800 Subject: [PATCH 1/2] allows validation to ignore self and update --- app/Providers/ValidationServiceProvider.php | 24 ++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php index 70fa64702e..fffb571eee 100644 --- a/app/Providers/ValidationServiceProvider.php +++ b/app/Providers/ValidationServiceProvider.php @@ -218,19 +218,37 @@ class ValidationServiceProvider extends ServiceProvider Validator::extend('is_unique_department', function ($attribute, $value, $parameters, $validator) { $data = $validator->getData(); - if ((array_key_exists('location_id', $data) && $data['location_id'] != null) && (array_key_exists('company_id', $data) && $data['company_id'] != null)) { + + if ( + array_key_exists('location_id', $data) && $data['location_id'] !== null && + array_key_exists('company_id', $data) && $data['company_id'] !== null + ) { + //for updating existing departments + if(array_key_exists('id', $data) && $data['id'] !== null){ + $count = Department::where('name', $data['name']) + ->where('location_id', $data['location_id']) + ->where('company_id', $data['company_id']) + ->whereNotNull('company_id') + ->whereNotNull('location_id') + ->where('id', '!=', $data['id']) + ->count(); + + return $count < 1; + }else // for entering in new departments + { $count = Department::where('name', $data['name']) ->where('location_id', $data['location_id']) ->where('company_id', $data['company_id']) ->whereNotNull('company_id') ->whereNotNull('location_id') - ->count('name'); + ->count(); return $count < 1; } + } else { return true; - } + } }); Validator::extend('not_array', function ($attribute, $value, $parameters, $validator) { From c9d46856a32047dec91e56eb9398ebd841ed9715 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 14 Nov 2023 15:00:11 -0800 Subject: [PATCH 2/2] added name back --- app/Providers/ValidationServiceProvider.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php index fffb571eee..7a8465f0d1 100644 --- a/app/Providers/ValidationServiceProvider.php +++ b/app/Providers/ValidationServiceProvider.php @@ -231,7 +231,7 @@ class ValidationServiceProvider extends ServiceProvider ->whereNotNull('company_id') ->whereNotNull('location_id') ->where('id', '!=', $data['id']) - ->count(); + ->count('name'); return $count < 1; }else // for entering in new departments @@ -241,7 +241,7 @@ class ValidationServiceProvider extends ServiceProvider ->where('company_id', $data['company_id']) ->whereNotNull('company_id') ->whereNotNull('location_id') - ->count(); + ->count('name'); return $count < 1; }