ldc-legaulois/app/Http/Requests/Bo/Meats/UpdateSubmodelsOrderRequest.php
2026-09-25 16:25:31 +02:00

48 lines
1.3 KiB
PHP

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