I have the following custom validtion rule for a voucher code which works and goes off to the server and validates my voucherCode correctly.
However there's 6 different failing states for a voucher - out of date, already redeemed, user not allowed etc etc.
I set the voucher message in the ajax response but this doesn't get propagated to the validation message.
Is it possible to have multiple messages for a single validation rule and change the message based on the result?
I couldn't find anything in the docs.
this.voucherMessage = "blah blah";
this.voucherCode = ko.observable("").extend({
validation: {
async: true,
validator: function(val, params, callback) {
var voucherCode;
voucherCode = val.replace("-", "");
return $.ajax({
url: constantsRoutes.vouchers.getInfo(voucherCode),
type: 'GET',
success: callback
}).done(function(response, statusText, xhr) {
var isValid;
_this.voucherDetails.setVoucher(response);
_this.voucherMessage = _this.voucherDetails.voucherState.display();
isValid = _this.voucherDetails.voucherState.state() === 0;
return callback(isValid);
});
},
message: this.voucherMessage
}
I should've read the source code in the first place - might be useful for someone else though
callback({
isValid: isValid,
message: _this.voucherDetails.voucherState.display()
});
This does the trick!