Knockout validation message based set in the validation function

て烟熏妆下的殇ゞ 提交于 2020-01-04 08:23:07

问题


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

is there a good way to do this?


回答1:


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/



来源:https://stackoverflow.com/questions/17582154/knockout-validation-message-based-set-in-the-validation-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!