49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Enums\TypeSondage;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class SondageResponsesRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function authorize()
|
|
{
|
|
return auth('employee')->check();
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
$responsesIds = $this->sondage->reponseSondages()->pluck('id')->all();
|
|
switch ($this->sondage->type) {
|
|
case TypeSondage::radio():
|
|
case TypeSondage::checkbox():
|
|
return [
|
|
'reponses' => 'required|array',
|
|
'reponses.*' => [
|
|
'required',
|
|
'numeric',
|
|
'exists:reponse_sondages,id',
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
function ($attribute, $value, $fail) use ($responsesIds) {
|
|
if (!in_array($value, $responsesIds)) {
|
|
$fail(trans('Une des réponses fournis ne fait pas partie du sondage'));
|
|
}
|
|
}
|
|
]
|
|
];
|
|
default:
|
|
abort(500, 'Type de sondage non géré');
|
|
}
|
|
}
|
|
}
|