0

I have a jsFiddle here:

There is an array in knockout viewmodel and every array element raises an error. Is there a way to count number of error messages for the whole view model and access it by a public javascript function?

The idea here is to count total errors of the viewmodel and then perform some action outside in a javascript public function.

Thanks

Nexus23
  • 6,195
  • 9
  • 50
  • 67
  • What is a public JavaScript function? Are you asking if you can access the view model objects outside of the view model or outside of window? – PW Kad Nov 11 '13 at 18:11
  • Yes. Explanation, I am using a web form and a custom validator on it. Now the custom validator client side function will determine if the view model is valid or not before postback. This client side function will be a simple public javascript function which needs to access if error length of the whole viewmodel is 0. – Nexus23 Nov 11 '13 at 18:27

2 Answers2

2

Try out this modified version of your fiddle: http://jsfiddle.net/js8hh/4/

        self.Errors = ko.computed(function() {
            var errs = [];
            for(var p in self) {
                if(self.hasOwnProperty(p)) {
                    var pObj = self[p];
                    if(!!!ko.validation.utils.isValidatable(self[p])) {
                        console.info("ope, nvm");
                    } else {                            
                        if(!ko.validation.validateObservable(pObj)) {
                            errs.push({'Property':p,'Error':ko.utils.unwrapObservable(pObj.error)});
                        }
                    }
                } else {
                    console.trace("skipping prototype property");
                }
            }
            return errs;
        });

and then at the bottom

            addJointHolder: addJointHolder,
            AllErrors: ko.computed(function() {
                result = []
                ko.utils.arrayForEach(jointholders(),function(jh) {
                    if(jh.Errors().length > 0) {
                        result.push({'JointHolder':jh,'Errors':jh.Errors()});
                    }
                });
                return result;
            })
scaryman
  • 1,880
  • 1
  • 19
  • 30
1

Why not use built in functionality instead?

ko.validation.group

It returns a observable array with the errors

http://jsfiddle.net/tRVCr/4/

Anders
  • 17,306
  • 10
  • 76
  • 144
  • Can you please help to count the errors in my fiddle through this builtin function? Thanks – Nexus23 Nov 12 '13 at 09:25
  • Thanks @Anders, but your solution is only calculating errors of first row. If jointholders are added on the page, the errors don't get accumulated. – Nexus23 Nov 12 '13 at 14:05
  • Ah, its a old bug in the library (It does not listen to array changes), I thought that was fixed, you can fix it like this http://jsfiddle.net/tRVCr/5/ – Anders Nov 12 '13 at 14:09
  • Thanks @Andres, better possible solution. Apart from remove functionality been broken it works well too. – Nexus23 Nov 12 '13 at 15:42