Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法自定义message? #15

Closed
zhoudaqing opened this issue Sep 16, 2016 · 9 comments · Fixed by #68
Closed

无法自定义message? #15

zhoudaqing opened this issue Sep 16, 2016 · 9 comments · Fixed by #68
Labels

Comments

@zhoudaqing
Copy link

zhoudaqing commented Sep 16, 2016

不支持自定义message,请参考https://github.com/jzaefferer/jquery-validation ,很多时候messages节点内提示的文案内容是自定义的

const loginRule = {
    rules: {
        username: {
            type: 'email'
        },
        password: {
            required: true
        }
    },
    messages: {
        username: {
            required: "请输入用户名.",
            email: "请输入正确的邮箱格式"
        },
        password: {
            required: "请输入如密码."
        },
    }
};
@duola8789
Copy link

+1
现在只有一些情况,自定义message可以生效,希望能做到每条校验首先读取是否有自定义的message,没有的话再读取默认的message

@blockmood
Copy link

@duola8789 麻烦问下自定义是如何生效的,请贴段代码,谢谢!

@duola8789
Copy link

duola8789 commented Jun 4, 2019

@duola8789 麻烦问下自定义是如何生效的,请贴段代码,谢谢!

像下面这样就可以实现,但是只有date/email/url等几种类型时才成功,可以看一下源码,只有几种类型可以传参

// 查询列表数据的日期的校验规则
const queryDateRule = {
  startTime: { type: 'date', trim: true, message: '查询日期错误' },
  endTime: { type: 'date', trim: true, message: '查询日期错误' },
}
class PrivacyController extends Controller {
  async index() {
    const { ctx } = this
    const { query } = ctx.request
    const { startTime = '', endTime = '', versionType = '' } = query

    // 参数校验
    ctx.validate(queryDateRule, ctx.request.query)
  }
}

@blockmood
Copy link

blockmood commented Jun 4, 2019

@duola8789 string int 无法实现是吗? 谢了,我去看看源码

@duola8789
Copy link

@blockmood 应该是不行

@nanzm
Copy link

nanzm commented Jun 20, 2019

+1 这个是需求 很关键

@fengmk2
Copy link
Member

fengmk2 commented Jun 20, 2019

欢迎 pr 来支持

sarike added a commit to sarike/parameter that referenced this issue Jun 22, 2019
@ckvv
Copy link

ckvv commented Aug 14, 2020

之前写的,添加了自定义的提示https://github.com/chenkai0520/node_modules/blob/master/parameter/index.js

fengmk2 pushed a commit that referenced this issue Mar 14, 2023
[skip ci]

## [3.7.0](v3.6.0...v3.7.0) (2023-03-14)

### Features

* custom error message ([#68](#68)) ([818cd23](818cd23)), closes [#31](#31) [#15](#15)
@github-actions
Copy link

🎉 This issue has been resolved in version 3.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants