adds validation for department names at Company locations

This commit is contained in:
Godfrey M 2022-11-09 13:11:33 -08:00
parent 7c21158680
commit 92ae271292
4 changed files with 22 additions and 1 deletions

View file

@ -29,7 +29,7 @@ class Department extends SnipeModel
];
protected $rules = [
'name' => 'required|max:255',
'name' => 'required|max:255|is_unique_department',
'location_id' => 'numeric|nullable',
'company_id' => 'numeric|nullable',
'manager_id' => 'numeric|nullable',

View file

@ -2,8 +2,10 @@
namespace App\Providers;
use App\Models\Department;
use DB;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Rule;
use Validator;
/**
@ -213,6 +215,23 @@ class ValidationServiceProvider extends ServiceProvider
return true;
}
});
Validator::extend('is_unique_department', function ($attribute, $value, $parameters, $validator) {
$data = $validator->getData();
if ($data['location_id'] != null && $data['company_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')
->count('name');
return $count < 1;
}
else {
return true;
}
});
}
/**

View file

@ -3,6 +3,7 @@
return array(
'does_not_exist' => 'Department does not exist.',
'department_already_exists' => 'A department already exists with that name at this company location. Or choose a more specific name for this department. ',
'assoc_users' => 'This department is currently associated with at least one user and cannot be deleted. Please update your users to no longer reference this department and try again. ',
'create' => array(
'error' => 'Department was not created, please try again.',

View file

@ -49,6 +49,7 @@ return [
'ip' => 'The :attribute must be a valid IP address.',
'ipv4' => 'The :attribute must be a valid IPv4 address.',
'ipv6' => 'The :attribute must be a valid IPv6 address.',
'is_unique_department' => 'The :attribute must be unique to this Company Location',
'json' => 'The :attribute must be a valid JSON string.',
'max' => [
'numeric' => 'The :attribute may not be greater than :max.',