2026-02-26 13:35:38 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文件功能:聊天室发言请求验证器
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author ChatRoom Laravel
|
|
|
|
|
|
*
|
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
|
2026-02-28 23:44:38 +08:00
|
|
|
|
use Illuminate\Contracts\Validation\Validator;
|
2026-02-26 13:35:38 +08:00
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2026-02-28 23:44:38 +08:00
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
2026-02-26 13:35:38 +08:00
|
|
|
|
|
|
|
|
|
|
class SendMessageRequest extends FormRequest
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function authorize(): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return true; // 权限验证已交由中间件处理
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function rules(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return [
|
|
|
|
|
|
'content' => ['required', 'string', 'max:500'], // 防止超长文本炸服
|
|
|
|
|
|
'to_user' => ['nullable', 'string', 'max:50'],
|
|
|
|
|
|
'is_secret' => ['nullable', 'boolean'],
|
|
|
|
|
|
'font_color' => ['nullable', 'string', 'max:10'], // html color hex
|
|
|
|
|
|
'action' => ['nullable', 'string', 'max:50'], // 动作(例如:微笑着说)
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function messages(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return [
|
|
|
|
|
|
'content.required' => '不能发送空消息。',
|
|
|
|
|
|
'content.max' => '发言内容不能超过 500 个字符。',
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
2026-02-28 23:44:38 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 重写验证失败的处理,无论如何(就算未按 ajax 标准提交)都必须抛出 JSON,不可以触发网页重定向去走 GET 请求而引发 302 方法错误
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function failedValidation(Validator $validator)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new HttpResponseException(response()->json([
|
|
|
|
|
|
'status' => 'error',
|
|
|
|
|
|
'message' => $validator->errors()->first(),
|
|
|
|
|
|
'errors' => $validator->errors(),
|
|
|
|
|
|
], 422));
|
|
|
|
|
|
}
|
2026-02-26 13:35:38 +08:00
|
|
|
|
}
|