56 lines
1.5 KiB
PHP
56 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\Student;
|
|
use App\Rules\UniqueFullNameAtSchool;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use function array_key_exists;
|
|
use function request;
|
|
|
|
class StudentStoreRequest extends FormRequest
|
|
{
|
|
public function rules(): array
|
|
{
|
|
$schoolId = $this->input('school_id', Auth::user()->school_id);
|
|
|
|
// If the user is not an admin, force their school_id to be used
|
|
if (! Auth::user()->is_admin) {
|
|
$schoolId = Auth::user()->school_id;
|
|
}
|
|
|
|
return [
|
|
'first_name' => ['required'],
|
|
'last_name' => [
|
|
'required',
|
|
new UniqueFullNameAtSchool(request('first_name'), request('last_name'), $schoolId),
|
|
],
|
|
'grade' => ['required', 'integer'],
|
|
'school_id' => ['sometimes', 'exists:schools,id'], // Only validates if present
|
|
'shirt_size' => [
|
|
'nullable',
|
|
function ($attribute, $value, $fail) {
|
|
if (! array_key_exists($value, Student::$shirtSizes)) {
|
|
$fail("The selected $attribute is invalid.");
|
|
}
|
|
},
|
|
],
|
|
];
|
|
}
|
|
|
|
// Helper method to get optional data (optional)
|
|
public function getOptionalData(): array
|
|
{
|
|
return ($this->shirt_size !== 'none')
|
|
? ['shirt_size' => $this->shirt_size]
|
|
: [];
|
|
}
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
}
|