【问题标题】:VeeValidate - custom validation functions in ES5VeeValidate - ES5 中的自定义验证函数
【发布时间】:2018-08-25 11:52:38
【问题描述】:

我正在使用 VeeValidate 创建自定义验证规则。官方文档为 getMessagevalidate 方法使用箭头函数。如何在常规函数语法中实现这些函数?

VeeValidate.Validator.extend('verify_username', {
  getMessage: field => 'Your username must be 3-24 characters long, \
    contains only a-z, 0-9, a period or an underscore, and should begin \
    with an alphabetic character.',
  validate: value => /^[a-z][a-z0-9._]{2,23}$/.test(value)
}); 

【问题讨论】:

    标签: javascript vue.js arrow-functions vee-validate


    【解决方案1】:

    如果你不想使用箭头函数,你可以在它的位置传递一个普通函数:

    VeeValidate.Validator.extend('verify_username', {
      getMessage: function (field) {
        return "username must be..."
      },
      validate: function (value) {
        return "[...]"
      }
    }); 
    

    这些功能是相同的:

    (foo) => 'bar'; 
    

    等同于:

    function (foo) {
      return 'bar'
    }
    

    【讨论】:

    猜你喜欢
    • 2020-04-30
    • 2020-03-10
    • 2018-12-07
    • 2020-07-23
    • 2010-10-01
    • 1970-01-01
    • 2019-07-02
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多