2026-02-26 13:35:38 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 文件功能:修改聊天室设置请求验证器
|
|
|
|
|
*
|
|
|
|
|
* @author ChatRoom Laravel
|
|
|
|
|
*
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2026-04-19 14:43:02 +08:00
|
|
|
use Illuminate\Validation\Rule;
|
2026-02-26 13:35:38 +08:00
|
|
|
|
2026-04-19 14:43:02 +08:00
|
|
|
/**
|
|
|
|
|
* 修改聊天室设置请求验证器
|
|
|
|
|
* 负责约束房间名称更新时的合法性,避免危险字符进入前端模板。
|
|
|
|
|
*/
|
2026-02-26 13:35:38 +08:00
|
|
|
class UpdateRoomRequest extends FormRequest
|
|
|
|
|
{
|
|
|
|
|
/**
|
2026-04-19 14:43:02 +08:00
|
|
|
* 判断当前请求是否允许继续。
|
2026-02-26 13:35:38 +08:00
|
|
|
*/
|
|
|
|
|
public function authorize(): bool
|
|
|
|
|
{
|
|
|
|
|
// 权限判断(是否为房主)会直接在 Controller/Policy 中进行
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-19 14:43:02 +08:00
|
|
|
* 返回修改房间设置的校验规则。
|
2026-02-26 13:35:38 +08:00
|
|
|
*
|
|
|
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
|
|
|
*/
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2026-04-19 14:43:02 +08:00
|
|
|
'name' => [
|
|
|
|
|
'required',
|
|
|
|
|
'string',
|
|
|
|
|
'max:50',
|
|
|
|
|
'regex:/^[^<>]+$/u',
|
|
|
|
|
Rule::unique('rooms', 'room_name')->ignore($this->route('id')),
|
|
|
|
|
],
|
2026-02-26 13:35:38 +08:00
|
|
|
'description' => ['nullable', 'string', 'max:255'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 14:43:02 +08:00
|
|
|
/**
|
|
|
|
|
* 在校验前整理更新表单,避免前后空白影响唯一性与安全判断。
|
|
|
|
|
*/
|
|
|
|
|
protected function prepareForValidation(): void
|
|
|
|
|
{
|
|
|
|
|
$name = $this->input('name');
|
|
|
|
|
$description = $this->input('description');
|
|
|
|
|
|
|
|
|
|
$this->merge([
|
|
|
|
|
'name' => is_string($name) ? trim($name) : $name,
|
|
|
|
|
'description' => is_string($description) ? trim($description) : $description,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回房间设置更新失败时的中文提示。
|
|
|
|
|
*/
|
2026-02-26 13:35:38 +08:00
|
|
|
public function messages(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'name.required' => '房间名称不能为空。',
|
|
|
|
|
'name.unique' => '该房间名称已存在。',
|
2026-04-19 14:43:02 +08:00
|
|
|
'name.regex' => '房间名称不能包含尖括号。',
|
2026-02-26 13:35:38 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|