clrghouz/app/Http/Requests/SystemRegister.php

66 lines
1.8 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Models\System;
class SystemRegister extends FormRequest
{
private System $so;
/**
* 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'))
return [];
if ((! $this->so->exists) && ($request->action == 'create')) {
return [
'name' => 'required|min:3',
];
}
return array_filter(array_merge(
[
'name' => 'required|min:3',
],
($this->so->exists || ($request->action != 'create')) ? [
'location' => 'required|min:3',
'sysop' => 'required|min:3',
'phone' => 'nullable|regex:/^([0-9-]+)$/',
'address' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i',
'port' => 'nullable|digits_between:2,5',
'method' => 'nullable|numeric',
'mailer_type' => 'nullable|numeric',
'mailer_address' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i',
'mailer_port' => 'nullable|digits_between:2,5',
'zt_id' => 'nullable|size:10|regex:/^([A-Fa-f0-9]){10}$/|unique:systems,zt_id,'.($this->so->exists ? $this->so->id : 0),
] : [],
$this->so->exists ? [
'active' => 'required|boolean',
'hold' => 'required|boolean',
] : [],
));
}
}