添加自定义验证错误信息 laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41658980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:07:46  来源:igfitidea点击:

Add a custom validation error message laravel

phplaravellaravel-5

提问by BOTJr.

I can do something like this to validate something on the controller.

我可以做这样的事情来验证控制器上的某些东西。

$this->validate($request,[
'myinput'=>'regex:some pattern'
]);

and the output of this would be something like this

并且它的输出将是这样的

The myinput format is invalid.

myinput 格式无效。

What i want was to show something of my own message

我想要的是展示我自己的信息

Only some pattern allowed

只允许某些模式

How do i achieve this on laravel?

我如何在 Laravel 上实现这一目标?

回答by nokieng

There are many techniques to customize validator messages.

有许多技术可以自定义验证器消息。

Validate inside the controller

在控制器内部验证

It would be looked like this

它看起来像这样

$validate = Validator::make($request->all(), [
    'name'=>'required|max:120',
    'email'=>'required|email|unique:users,email,'.$id,
    'password'=>'nullable|min:6|confirmed'
], 
[
    'name.required' => 'User name must not be empty!',
    'name.max' => 'The maximun length of The User name must not exceed :max',
    'name.regex' => 'Use name can not contain space',
    'email.required' => 'Email must not be empty!',
    'email.email' => 'Incorrect email address!',
    'email.unique' => 'The email has already been used',
    'password.min' => 'Password must contain at least 6 characters',
    'password.confirmed' => 'Failed to confirm password'
]);

  • The first param is inputs to validate
  • 第一个参数是要验证的输入
  • The second array is validator rules
  • 第二个数组是验证器规则
  • The last params is the customized validator messages
  • 最后一个参数是自定义的验证器消息
  • 其中,同步语法是 [input_variable_name].[validator_name] => "Customized message"[input_variable_name].[validator_name] => "自定义消息"

    Second apprach: using InfyOm Laravel Generator

    第二种方法:使用 InfyOm Laravel Generator

    I like this approach the most. It provides useful tools for generating such as Controller, Models, Views, API etc. And yet, create and update Requestfile. in which the Request file is using Illuminate\Foundation\Http\FormRequestwhere this class is extended from Illuminate\Http\Request.

    It is mean that we can access Requestin this file and perform validating for the incoming requests.
    Here is the most interesting part to me.
    The generated request file contains rulesfunction, for example like this

    我最喜欢这种方法。它提供了有用的工具来生成诸如控制器、模型、视图、API 等。然而,创建和更新Request文件。其中请求文件正在使用Illuminate\Foundation\Http\FormRequest,该类是从Illuminate\Http\Request.

    这意味着我们可以访问该文件中的Request并对传入的请求执行验证。
    这是对我来说最有趣的部分。
    生成的请求文件包含规则函数,例如像这样

    public function rules() {
        return [
            'name' => 'required|unique:flights,name|max:20',
            'airline_id' => 'nullable|numeric|digits_between:1,10',
        ];
    }
    

    This function actually returns the validator-rules and validate them against the inputs. And you can override function messagesfrom Illuminate\Foundation\Http\FormRequestto customize the error message as you need:

    这个函数实际上返回验证器规则并根据输入验证它们。您可以覆盖来自Illuminate\Foundation\Http\FormRequest 的函数消息以根据需要自定义错误消息:

    public function messages()
    {
        return [
            'required' => "This field is required",
            \... etc
        ];
    }
    



    Nonetheless, you can do many nore with the generated request files, just refer to file in vendor folder vendor/laravel/framework/src/Illuminate/Foundation/Httpfrom your project.

    Here is the Infyom github link InfyOmLabs - laravel-generator



    尽管如此,您可以对生成的请求文件做很多事情,只需vendor/laravel/framework/src/Illuminate/Foundation/Http从您的项目中引用 vendor 文件夹中的文件即可。

    这是 Infyom github 链接InfyOmLabs - laravel-generator

    回答by Alexey Mezenin

    You can add custom validation messages to language files, like resources/lang/en/validation.php.

    您可以将自定义验证消息添加到语言文件,例如resources/lang/en/validation.php.

    Another way to do that, from docs:

    另一种方法,从文档:

    'custom' => [
        'email' => [
            'regex' => 'Please use your company email address to register. Webmail services are not permitted.'
        ],
        'lawyer_legal_fields' => [
            'number_of_areas' => 'You\'re not allowed to select so many practice areas'
        ],
    ],
    

    You may customize the error messages used by the form request by overriding the messages method.

    您可以通过覆盖 messages 方法自定义表单请求使用的错误消息。

    public function messages()
    {
        return [
            'title.required' => 'A title is required',
            'body.required'  => 'A message is required',
        ];
    }
    

    https://laravel.com/docs/5.3/validation#customizing-the-error-messages

    https://laravel.com/docs/5.3/validation#customizing-the-error-messages