74 lines
2 KiB
PHP
74 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Club\Surveys;
|
|
|
|
use App\Enums\News\NewsClubTypeEnum;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class StoreSurveyAnswerRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Auth::guard('club')->check();
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'news_club_id' => intval($this->news_club->getKey()),
|
|
'answers' => collect($this->answers)->map(function ($value) {
|
|
return intval($value);
|
|
})->toArray(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'news_club_id' => 'required|integer|exists:news_clubs,id',
|
|
'answers' => [
|
|
'required',
|
|
'array',
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
function (string $attribute, $value, callable $fail) {
|
|
if (
|
|
$this->news_club->type->equals(NewsClubTypeEnum::survey)
|
|
&& !$this->news_club->multiple_answers && count($value) > 1
|
|
) {
|
|
$fail(trans('Une seule réponse est autorisée pour ce sondage'));
|
|
}
|
|
}
|
|
],
|
|
'answers.*' => 'required|integer|exists:surveys_answers,id',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'news_club_id' => trans_choice('models.classes.news_club', 1),
|
|
'answers' => trans_choice('models.classes.surveys_answer', 1),
|
|
];
|
|
}
|
|
}
|