77 lines
2.3 KiB
PHP
77 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class DoublerRequestsStoreRequest extends FormRequest
|
|
{
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'doubler_requests' => ['required', 'array'],
|
|
|
|
// Validate event IDs (first keys)
|
|
'doubler_requests.*' => ['required', 'array'],
|
|
|
|
// Validate student IDs (second keys) and their values
|
|
'doubler_requests.*.*' => [
|
|
'required',
|
|
'string',
|
|
'max:50',
|
|
// Custom validation rule to check if the student ID exists in DB
|
|
function ($attribute, $value, $fail) {
|
|
// $attribute will be like "doubler_requests.1.107"
|
|
// Extract event_id and student_id from attribute
|
|
preg_match('/doubler_requests\.(\d+)\.(\d+)/', $attribute, $matches);
|
|
if (! $matches) {
|
|
$fail('Invalid attribute format.');
|
|
|
|
return;
|
|
}
|
|
$eventId = $matches[1];
|
|
$studentId = $matches[2];
|
|
|
|
// Check event ID exists
|
|
if (! \App\Models\Event::where('id', $eventId)->exists()) {
|
|
$fail("Event ID {$eventId} does not exist.");
|
|
|
|
return;
|
|
}
|
|
|
|
// Check student ID exists
|
|
if (! \App\Models\Student::where('id', $studentId)->exists()) {
|
|
$fail("Student ID {$studentId} does not exist.");
|
|
|
|
return;
|
|
}
|
|
},
|
|
],
|
|
];
|
|
}
|
|
|
|
public function getDoublerRequests(): array
|
|
{
|
|
$validated = $this->validated()['doubler_requests'] ?? [];
|
|
|
|
$result = [];
|
|
|
|
foreach ($validated as $eventId => $students) {
|
|
foreach ($students as $studentId => $request) {
|
|
$result[] = [
|
|
'event_id' => (int) $eventId,
|
|
'student_id' => (int) $studentId,
|
|
'request' => $request,
|
|
];
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
}
|