Dhanuka777
Dhanuka777

Reputation: 8636

Aggregate multiple max KO extend validations

I am using ko extend validation to validate, Trying to do something like follows. ko.extend does not support multiple bindings for same type (max in this case) and it only adds first rule. Is there a way to conditionally pass the params with a function? or my only way out is writing a custom validation?

         myModel.prop2.extend({
            max: {
                onlyIf: function () {

                    var test = myModel.prop1() != undefined &&
                        myModel.prop1() !== "Percentage";
                    return test;
                },
                params: 100000
            }
        }).extend({
            max: {
                onlyIf: function() {
                    return myModel.prop1() != undefined &&
                        myModel.prop1() === "Percentage";
                },
                params: 100
            }
        });

Upvotes: 1

Views: 352

Answers (2)

Rajeshkumar Kandhasamy
Rajeshkumar Kandhasamy

Reputation: 6461

I'm not sure, what you are really trying to achieve. But the way that you tried is not valid way i think. So, you May have to use something like below,

Solution 1:

You can supply a validator or an array of them.

var testObj = ko.observable(3).extend({
        validation: [{
            validator: function (val, someOtherVal) {
                return val === someOtherVal;
            },
            message: 'Must Equal 5',
            params: 5
        },{
            validator: function (val) {
                return val !== 8;
            },
            message: 'Can not be 8',
            params: 8
        }]
    });

Solution 2:

ko.validation.rules['compareTo'] = {
    message: "Compare to",
    validator: function (val, params) {
        if (val === null || params.value() === null)
            return params.allowNull;
        switch (params.way) {
            case "<": return val < params.value();
            case "<=": return val <= params.value();
            case ">": return val > params.value();
            case ">=": return val >= params.value();
            default: throw new Error("params is not well defined");
        }
    }
}

myModel.prop2.extend({ compareTo: { 
                       message:"your message", 
                       params: { 
                                   way: ">=", 
                                   value: model.otherObs, 
                                   allowNull: true 
                       }, 
                       onlyIf: function () { /*your test*/ } 
                    }});

Hope this helps.,

Upvotes: 1

Dhanuka777
Dhanuka777

Reputation: 8636

Found a solution,

.extend({
            validation: [{
                validator: function (val, someOtherVal) {
                    if (myModel.prop1()() !== "Percentage") {
                        return val < someOtherVal;
                    }
                    return true;
                },
                message: "Too big!!!!",
                params: 100000
            }]
        })

Upvotes: 0

Related Questions