简体   繁体   English

Laravel:自定义表单请求中的自定义验证消息

[英]Laravel: Custom validation messages in Custom Form Requests

I am trying to keep my controller clean and move the custom request validation in to a separate class as: 我试图保持控制器整洁,并将自定义请求验证移至一个单独的类中,如下所示:

public function register(RegisterUserRequest $request)

and in there define all the usual functions, such as 并在其中定义所有常用功能,例如

public function rules(),
public function messages(), and
public function authorize()

However, the frontend is expecting the following data to display: title (title related to the validation error message), description (which is the the actual validation error message), and status (=red, yellow etc) 但是,前端希望显示以下数据: title (与验证错误消息相关的标题), description (这是实际的验证错误消息)和status (=红色,黄色等)

How can I actually customise the response of the request? 我实际上如何自定义请求的响应?

Something like this, does not seem to be working: 像这样的东西似乎不起作用:

protected function  failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
        {
            $response = new Response(['data' => [],
                'meta' => [
                    'title' => 'Email Invalid'
                    'description' => '(The error message as being returned right now)',
                    'status' => 'red'
                ]);

            throw new ValidationException($validator, $response);

    }

Any ideas? 有任何想法吗?

you can do this by making request in laravel as the following : 您可以通过在laravel中进行如下请求来做到这一点:

php artisan make:request FailedValidationRequest php artisan make:request FailedValidationRequest

this command will create class called FailedValidationRequest in 该命令将在以下目录中创建名为FailedValidationRequest的类

App\\Http\\Request directory and you cab write your rules inside this class as the following: App \\ Http \\ Request目录,然后您可以在此类中编写规则,如下所示:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class FailedValidationRequest extends FormRequest
{
/**
 * 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 [
       'title' => 'required'
       'description' => 'required',
       'status' => 'required|numeric'
    ];
}

} }

and if you want to customize the error message you can download the language you want from this link using composer: https://packagist.org/packages/caouecs/laravel-lang 如果要自定义错误消息,则可以使用composer从此链接下载所需的语言: https : //packagist.org/packages/caouecs/laravel-lang

and write the language you want by copying the languge folwder to the lang directory in your resource folder. 并通过将语言版本复制到资源文件夹中的lang目录中来编写所需的语言。

'custom' => [
    'title' => [
        'required' => 'your custom message goes here',
    ],
 'decription' => [
        'required' => 'your custom message goes here',
    ],
   ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM