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

时间:2015-04-18 08:16:48

标签: php validation laravel laravel-5

我想在laravel5表单验证错误消息中设置自定义字段名称。

我的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;
    }

}

这里当old,new和cnew emty,错误信息将如下,

  • 旧场是必需的。
  • 新字段是必需的。
  • 需要cnew字段。

我希望显示如下,而不是上面的消息,

  • 需要旧密码字段
  • 需要新密码字段
  • 需要确认密码字段。

如何在laravel5表单请求验证方法中使用?

1 个答案:

答案 0 :(得分:3)

选项1:

您可以在 自定义验证属性 '属性'下的资源/ lang / en / validation.php中定义自定义属性。 => [],像这样:

/*
    |--------------------------------------------------------------------------
    | 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'
    ]