如何在 laravel 5.8 中显示自定义消息?

发布于 2025-01-14 10:53:35 字数 1346 浏览 4 评论 0原文

我正在使用 laravel 5.8 版本,我有一个 api 负责注册,我创建一个请求文件,其中包含规则()和 messages()函数来显示错误消息,但如果任何验证失败,它不会抛出任何错误消息,为什么会这样正在发生有人可以解释吗?

UserController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests\userRequest;
use App\UserSection;

class UserController extends Controller
{
    public function userRegister(userRequest $request){

        //logic of my code                    
        return response()->json($success);
    }
}

userRequest.php

<?php

namespace App\Http\Requests;

use App\Rules\CustomRule;
use Illuminate\Foundation\Http\FormRequest;

class userRequest extends FormRequest
{

    public function messages()
    {
        return [
            'first_name.required' => 'A title is required',
        ];
    }
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    
    public function rules()
    {
        return [
            'first_name' => 'string|required|max:25',
            'phone_number' => 'required|integer'
        ];
    }

}

当我点击没有first_name键的路线时,我遇到的错误是显示404未找到错误

I am using laravel 5.8 version, i have one api which is responsible for registering ,i create one Request file which contains rules() and messages() function to display error messages but it's not throwing any error messages if any validation fails ,why this is happening can somebody explain ?

UserController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests\userRequest;
use App\UserSection;

class UserController extends Controller
{
    public function userRegister(userRequest $request){

        //logic of my code                    
        return response()->json($success);
    }
}

userRequest.php

<?php

namespace App\Http\Requests;

use App\Rules\CustomRule;
use Illuminate\Foundation\Http\FormRequest;

class userRequest extends FormRequest
{

    public function messages()
    {
        return [
            'first_name.required' => 'A title is required',
        ];
    }
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    
    public function rules()
    {
        return [
            'first_name' => 'string|required|max:25',
            'phone_number' => 'required|integer'
        ];
    }

}

The error i am facing when i hit the route without first_name key it's showing 404 not found error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2025-01-21 10:53:35

您可能错过了获取表单数据的标题部分

Accept = application/json

you might have missed headers part for taking the form-data

Accept = application/json
流绪微梦 2025-01-21 10:53:35

作为 laravel 文档

如果验证失败,将生成重定向响应以发送
用户返回到之前的位置。错误也会闪现
到会话,以便它们可供显示。如果请求是
AJAX 请求,带有 422 状态代码的 HTTP 响应将是
返回给用户,包括验证的 JSON 表示形式
错误。

因此,您需要指定您期望的响应类型,如果您使用 postman 来测试您的 api 端点,则必须在请求标头中添加 Accept:application/json

<?php

namespace App\Http\Controllers;

use App\Http\Requests\userRequest;
use App\UserSection;

class UserController extends Controller
{
    public function userRegister(userRequest $request){
        //logic of my code                    
        return response()->json($success);
    }
}

As laravel docs

If validation fails, a redirect response will be generated to send the
user back to their previous location. The errors will also be flashed
to the session so they are available for display. If the request was
an AJAX request, a HTTP response with a 422 status code will be
returned to the user including a JSON representation of the validation
errors.

So you need to specify the response type you expect, if you use postman for testing your api end point you have to add in request header Accept:application/json

<?php

namespace App\Http\Controllers;

use App\Http\Requests\userRequest;
use App\UserSection;

class UserController extends Controller
{
    public function userRegister(userRequest $request){
        //logic of my code                    
        return response()->json($success);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文