Files
scrum-poker-internal/app/Http/Requests/JoinRoomRequest.php
T

55 lines
1.5 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Models\Player;
use App\Models\Room;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class JoinRoomRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
protected function prepareForValidation(): void
{
$this->merge([
'join_code' => strtoupper(trim((string) $this->input('join_code'))),
'player_name' => trim((string) $this->input('player_name')),
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$roomId = Room::query()
->where('join_code', $this->input('join_code'))
->value('id');
return [
'join_code' => ['bail', 'string', 'required', 'size:6', Rule::exists(Room::class, 'join_code')],
'player_name' => ['required', 'string', 'max:50', Rule::unique(Player::class, 'name')->where('room_id', $roomId)],
];
}
public function messages(): array
{
return [
'join_code.exists' => 'Dieser Raumcode existiert nicht.',
'join_code.size' => 'Der Raumcode muss aus sechs Zeichen bestehen.',
'player_name.unique' => 'Dieser Name wird in dem Raum bereits verwendet.',
];
}
}