auditionadmin/app/Http/Requests/SubscoreDefinitionRequest.php

61 lines
1.8 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class SubscoreDefinitionRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return auth()->user()->is_admin; // Only allow admins to create/update subscores
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
protected function prepareForValidation()
{
// Handle checkboxes
$this->merge([
'for_seating' => $this->has('for_seating') ? true : false,
'for_advance' => $this->has('for_advance') ? true : false,
]);
}
public function rules(): array
{
$guideId = $this->route('guide')->id; // get the guide ID from route model binding
$definition = $this->route('subscore')->id ?? null; // get the guide ID from route model binding
return [
'name' => [
'required',
'string',
Rule::unique('subscore_definitions') // name must be unique on a guide, allow for the current name
->where(fn ($query) => $query->where('scoring_guide_id', $guideId))
->ignore($definition),
],
'max_score' => ['required', 'integer'],
'weight' => ['required', 'integer'],
'for_seating' => ['boolean'],
'for_advance' => ['boolean'],
];
}
protected function passedValidation()
{
// Apply your custom logic
if (! auditionSetting('advanceTo')) {
$this->merge(['for_seating' => true]);
}
}
}