Files
chatroom/app/Http/Requests/SendMessageRequest.php

75 lines
2.5 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* 文件功能:聊天室发言请求验证器
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
2026-04-12 14:04:18 +08:00
/**
* 聊天室发言请求验证器
* 负责统一校验文本消息与图片消息的发送参数。
*/
class SendMessageRequest extends FormRequest
{
/**
2026-04-12 14:04:18 +08:00
* 判断当前请求是否允许继续。
*/
public function authorize(): bool
{
return true; // 权限验证已交由中间件处理
}
/**
2026-04-12 14:04:18 +08:00
* 返回发言请求的校验规则。
*
2026-04-12 14:04:18 +08:00
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<int, mixed>|string>
*/
public function rules(): array
{
return [
2026-04-12 14:04:18 +08:00
'content' => ['nullable', 'required_without:image', 'string', 'max:500'], // 文本与图片至少二选一
'image' => ['nullable', 'required_without:content', 'file', 'image', 'mimes:jpeg,png,jpg,gif,webp', 'max:6144'],
'to_user' => ['nullable', 'string', 'max:50'],
'is_secret' => ['nullable', 'boolean'],
'font_color' => ['nullable', 'string', 'max:10'], // html color hex
'action' => ['nullable', 'string', 'max:50'], // 动作(例如:微笑着说)
];
}
2026-04-12 14:04:18 +08:00
/**
* 返回校验失败时的中文提示。
*/
public function messages(): array
{
return [
2026-04-12 14:04:18 +08:00
'content.required_without' => '文字内容和图片至少要发送一项。',
'content.max' => '发言内容不能超过 500 个字符。',
2026-04-12 14:04:18 +08:00
'image.required_without' => '文字内容和图片至少要发送一项。',
'image.image' => '上传的文件必须是图片。',
'image.mimes' => '仅支持 jpg、jpeg、png、gif、webp 图片格式。',
'image.max' => '图片大小不能超过 6MB。',
];
}
/**
* 重写验证失败的处理,无论如何(就算未按 ajax 标准提交)都必须抛出 JSON不可以触发网页重定向去走 GET 请求而引发 302 方法错误
*/
2026-04-12 14:04:18 +08:00
protected function failedValidation(Validator $validator): void
{
throw new HttpResponseException(response()->json([
'status' => 'error',
'message' => $validator->errors()->first(),
'errors' => $validator->errors(),
], 422));
}
}