clrghouz/app/Http/Requests/AddressMerge.php

51 lines
1.2 KiB
PHP
Raw Normal View History

2023-09-08 14:07:46 +00:00
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Models\Address;
class AddressMerge extends FormRequest
{
protected $stopOnFirstFailure = TRUE;
public function authorize()
{
return Gate::allows( 'admin');
}
public function rules(Request $request)
{
if (! $request->isMethod('post'))
return [];
return [
'src' => [
'required',
'exists:addresses,id',
],
'dst' => [
'required',
'exists:addresses,id',
'different:src',
function ($attribute,$value,$fail) use ($request) {
$dst = Address::withTrashed()->findOrFail($value);
$src = Address::withTrashed()->findOrFail($request->src);
if ((! $dst->active) && ($dst->system_id !== $src->system_id) && ($src->system->name !== 'Discovered System'))
$fail('Destination must be active, or be from the system system');
},
function ($attribute,$value,$fail) use ($request) {
$dst = Address::withTrashed()->findOrFail($value);
$src = Address::withTrashed()->findOrFail($request->src);
if ($src->ftn !== $dst->ftn)
$fail('Source and Destination must be the same FTN');
},
],
];
}
}