Fixed #18148 and #17451 - return int for user_count, fixed validation

This commit is contained in:
snipe
2025-11-04 15:56:23 +00:00
parent e906d25776
commit ddb031f091
5 changed files with 42 additions and 46 deletions
+24 -29
View File
@@ -283,41 +283,36 @@ class ValidationServiceProvider extends ServiceProvider
}
});
Validator::extend('is_unique_department', function ($attribute, $value, $parameters, $validator) {
/**
* Check that the 'name' field is unique in the table while within both company_id and location_id
* This is only used by Departments right now, but could be used elsewhere in the future.
*/
Validator::extend('is_unique_across_company_and_location', function ($attribute, $value, $parameters, $validator) {
$data = $validator->getData();
$table = array_get($parameters, 0);
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('name');
$count = DB::table($table)->select($attribute)
->where($attribute, $value)
->whereNull('deleted_at');
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');
return $count < 1;
if (array_key_exists('id', $data) && $data['id'] !== null) {
$count = $count->where('id', '!=', $data['id']);
}
}
else {
return true;
}
if (array_key_exists('location_id', $data) && $data['location_id'] !== null) {
$count = $count->where('location_id', $data['location_id']);
}
if (array_key_exists('company_id', $data) && $data['company_id'] !== null) {
$count = $count->where('company_id', $data['company_id']);
}
$count = $count->count('name');
return $count < 1;
});
Validator::extend('not_array', function ($attribute, $value, $parameters, $validator) {
return !is_array($value);
});