mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-24 05:04:07 -08:00
adds validation for department names at Company locations
This commit is contained in:
parent
7c21158680
commit
92ae271292
|
@ -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',
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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.',
|
||||
|
|
|
@ -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.',
|
||||
|
|
Loading…
Reference in a new issue