验证功能中基于敲除验证消息的设置 [英] Knockout validation message based set in the validation function

查看:83
本文介绍了验证功能中基于敲除验证消息的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置要在敲除的验证功能中显示的消息,类似于此处发生的情况:

I want to set the message to display in the validation function of knockout similar to whats going on here: Knockout Validation Plugin Custom Error Message but without async.

这里是我尝试的内容,但未显示验证消息.

Heres what ive tried, but no validation message is displayed.

this.name = ko.observable().extend({
    validation: {
        validator: function (val) {
            return { isValid:val === 'a', message: 'the value ' + val + ' is not a' };
        },
        message: 'I dont want this default message'
    }
});

JSFiddle

有什么好方法吗?

推荐答案

关闭,如果规则通过,验证器应返回true/false.我无法显示message:来显示值(即使设置是具有undefined参数的函数),因此如果您想向用户显示该值,则始终可以内联错误消息.

close, the validator should be returning true/false if the rule passed. I couldn't get the message: to display the value (even setting is as a function had undefined arguments) so you can always inline the error message if you want to display the value back to the user.

this.name = ko.observable().extend({
    validation: {
        validator: function (val) {
            if (val !== 'a') {
                this.message = 'the value ' + val + ' is not a';
                return false;
            }
            return true;
        }
    }
});

http://jsfiddle.net/gEwEX/10/

这篇关于验证功能中基于敲除验证消息的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆