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

这是我的request验证器代码

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

进行测试

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

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

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 11

场景里面没加吧

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

Exception handler那有啥处理吗

1年前 评论

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

<?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());
        }
    }
}
1年前 评论
陈先生

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

1年前 评论

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

1年前 评论

文件的173行截个图看下

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

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

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());
}
1年前 评论

验证方法能贴出来吗

1年前 评论

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