简体   繁体   English

为laravel5表单验证中的字段设置自定义验证消息?

[英]Set custom validation message for field in laravel5 form validation?

I want to set custom filed name in laravel5 form validation error messages. 我想在laravel5表单验证错误消息中设置自定义文件名。

my form validation request class is, 我的form validation request class

   class PasswordRequest extends Request {

    protected $rules = [
        'old' => ['required'],
        'new' => ['required','same:cnew'],
        'cnew' => ['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 $this->rules;
    }

}

Here when old,new and cnew emty, Error message will be like following, 在这里,当旧的,新的和新的空时,错误消息将如下所示,

  • The old field is required. 旧字段为必填项。
  • The new field is required. 必须填写新字段。
  • The cnew field is required. cnew字段是必填字段。

I want to display like following instead of above message, 我想显示如下而不是上面的消息,

  • Old password field is required 旧密码字段为必填项
  • New password field is required 必须输入新密码
  • Confirmation password field is required. 确认密码字段为必填项。

How it is possible in laravel5 Form Request Validation method? laravel5表单请求验证方法中怎么可能?

Option 1: 选项1:

You can define your custom attributes in resources/lang/en/validation.php under Custom Validation Attributes 'attributes' => [], like so: 您可以在resources / lang / en / validation.php中的Custom Validation Attributes'attributes '=> []下定义您的自定义属性,如下所示:

/*
    |--------------------------------------------------------------------------
    | Custom Validation Attributes
    |--------------------------------------------------------------------------
    |
    | The following language lines are used to swap attribute place-holders
    | with something more reader friendly such as E-Mail Address instead
    | of "email". This simply helps us make messages a little cleaner.
    |
    */

    'attributes' => [
        'old'              =>'Old Paaword',
        'new'              =>'New password',
        'cnew'             =>'Confirmation password'
    ]

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

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