讨论数量:
我的做法是在Request
目录下创建一个基类,然后使用字符串替换来消除空格,代码如下:
<?php
namespace App\Http\Requests;
use App\Exceptions\BusinessException;
use App\Helpers\ApiResponse;
use App\Helpers\ResponseEnum;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
class BaseRequest extends FormRequest
{
use ApiResponse;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* @throws BusinessException
*/
protected function failedValidation(Validator $validator)
{
$errorMsg = $validator->errors()->first();
// 将空格和句号替换成空
$info = str_replace([' ', '。'],'',$errorMsg);
$this->throwBusinessException(ResponseEnum::HTTP_ERROR, $info);
}
}
推荐文章: