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; } }