ldc-legaulois/app/Http/Requests/Bo/Ranges/UpdateSubmodelsOrderRequest.php

54 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Requests\Bo\Ranges;
use App\Models\Range;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
class UpdateSubmodelsOrderRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return Gate::check('reorderSubmodels', Range::class);
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
{
$this->merge([
'newValues' => json_decode($this->input('jsonValue'), true),
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
$currentRange = request('range');
return [
'jsonValue' => 'json',
'newValues' => 'required|array',
'newValues.*' => 'required|array',
'newValues.*.id' => 'required|exists:subranges,id',
'newValues.*.range_id' => function (string $attribute, mixed $value, \Closure $fail) use ($currentRange) {
if ($value !== $currentRange->id) {
$fail("The {$attribute} is invalid.");
}
},
];
}
}