验证器中messages定义的内容未生效

这是我的request验证器代码

验证器中messages定义的内容未生效

进行测试

验证器中messages定义的内容未生效

结果:
并没有按照我定义的错误信息进行返回,是什么原因呢?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 11

场景里面没加吧

2年前 评论
晏南风 (楼主) 2年前
sabine (作者) 2年前

Exception handler那有啥处理吗

2年前 评论

这是我得验证基类,供参考

<?php

namespace App\Admin\Requests;

use App\Exceptions\ParameterException;
use Illuminate\Foundation\Http\FormRequest;

class BaseRequest extends FormRequest
{
    /**
     * 单个验证失败后停止
     * @var bool
     */
    protected $stopOnFirstFailure = true;

    /**
     * Determine if the admin is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * 验证完毕后
     *
     * @param $validator
     * @return void
     * @throws ParameterException
     */
    public function withValidator($validator): void
    {
        if ($validator->fails()) {
            throw new ParameterException($validator->errors()->first());
        }
    }
}
2年前 评论

我猜你的错误抑制输出的错误信息不是取得 errors 的。。

2年前 评论

debug看看执行到哪一步了报的错误?

2年前 评论

文件的173行截个图看下

2年前 评论
deatil (作者) 2年前

看下你全局异常处理里面针对表单验证的是否正确

public function render(Throwable $exception)
{
    ..........
    // 表单验证错误
    if ($exception instanceof ValidationException) {
        return $this->error($exception->validator->errors()->first());
    }
}

如果针对表单请求验证类中做了特殊的处理 如不抛出 ValidationException异常 更换成项目中自己的异常的情况下 可以这样处理

protected function failedValidation(Validator $validator)
{
    throw new ApiException($validator->errors()->first());
}
2年前 评论

验证方法能贴出来吗

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!