Fix for system registration, new systems couldnt be added.

This commit is contained in:
2023-04-14 19:47:33 +10:00
parent 51e915b73d
commit c5500020ae
3 changed files with 29 additions and 27 deletions

View File

@@ -12,39 +12,41 @@ class SystemRegister extends FormRequest
{
private System $so;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(Request $request)
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(Request $request)
{
$this->so = System::findOrNew($request->system_id);
return Gate::allows($this->so->users->count() ? 'update' : 'register',$this->so);
}
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(Request $request)
{
if ((! $request->isMethod('post')) || ($request->action == 'register'))
/**
* Get the validation rules that apply to the request.
*
* If the system exists (POST & action="register" & system_id=<value>), then no validation required
* If the system doesnt exist (POST & action="register" & system_id undefined) then we need just a name to start the process (action="create")
* Then, full validation
* @return array
*/
public function rules(Request $request)
{
if ((! $request->isMethod('post')) || ($request->action === 'register'))
return [];
if ((! $this->so->exists) && ($request->action == 'create')) {
if ((! $this->so->exists) && ($request->action === 'create'))
return [
'name' => 'required|min:3',
];
}
return array_filter(array_merge(
return array_filter(array_merge(
[
'name' => 'required|min:3',
],
($this->so->exists || ($request->action != 'create')) ? [
($this->so->exists || ($request->action !== 'create')) ? [
'location' => 'required|min:3',
'sysop' => 'required|min:3',
@@ -62,5 +64,5 @@ class SystemRegister extends FormRequest
'hold' => 'required|boolean',
] : [],
));
}
}
}