60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class UpdateOrderRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Attribute model needed.
|
|
*
|
|
* @var Model $orderable
|
|
*/
|
|
private Model $orderableModel;
|
|
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return auth('backend')->check();
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->orderableModel = collect($this->route()->parameters())
|
|
->first(fn($param) => $param instanceof Model);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'order' => [
|
|
'required',
|
|
'integer',
|
|
'min:1',
|
|
sprintf('max:%s', $this->orderableModel->max('order')),
|
|
function ($attribute, $value, $fail) {
|
|
/** @phpstan-ignore-next-line */
|
|
if ($this->orderableModel && intval($value) === $this->orderableModel->order) {
|
|
$fail("La valeur de {$attribute} doit être différente de la valeur actuelle.");
|
|
}
|
|
},
|
|
],
|
|
];
|
|
}
|
|
}
|