2

In my razor view that using knockout and supposedly knockout validation I add the following line (to actually start using ko validation):

<script src="@Url.Content("~/Scripts/knockout.validation.debug.js")" type="text/javascript"></script>
  • When I run this view in Chrome validation is working perfectly.
  • When I run this view in IE (9.0), I get pretty ugly message saying the following:

Error Message

0x800a138f - Microsoft JScript runtime error: Unable to get value of the property 'rules': object is null or undefined.

(in text for future searches on SO) After hitting "Continue" it works well and validation seems to be working fine. Its just this message.

Worth mentioning that I searched through the solution - I don't use "rules" in no place. Not sure what is going on with IE.

Does anybody have any idea as to why would it happen and how to eliminate the error?

Display Name
  • 4,672
  • 1
  • 33
  • 43
  • The error is on line 70 of `knockout.validation.debug.js`, which I'd like to look at. I downloaded a different version of Knockout Validation -- can you upload your copy of `knockout.validation.debug.js`? – Evan Hahn Mar 12 '13 at 01:10
  • @EvanHahn Good one, thank you. Here goes: http://pastebin.com/GECfEpht – Display Name Mar 12 '13 at 01:12

1 Answers1

3

Short answer: it looks like the new version of Knockout Validation fixes this. Update your Knockout Validation to solve this problem and forget this ever happened.

Longer answer: the error comes from a bug in an internal utility method called isValidatable. Here it is, copy-pasted from the source:

isValidatable: function (o) {
    return o.rules && o.isValid && o.isModified;
}

Someone calls isValidatable where o is "null or undefined", as the error says. An object isn't validatable if it's not even an object! We hit an error in this case, because we're looking for o.rules, and that'll throw an error because o is undefined or null.

The new version of Knockout Validation does this:

isValidatable: function (o) {
    return o && o.rules && o.isValid && o.isModified;
},

That first clause returns true (technically, it returns o, which evaluates to true) if the object exists. If the object doesn't exist, it returns without an error if o is undefined.

If you're curious, here's the commit that fixed the bug you're experiencing.

Evan Hahn
  • 12,147
  • 9
  • 41
  • 59
  • I saw that line I have no idea who's calling it since I don't use any `rules` ... but short response to the short answer: let me try. But this is really weird, since I installed knockout validation 6 hours ago for the first time. Anyway, let me try. I'll be back – Display Name Mar 12 '13 at 01:31
  • The `rules` issue comes from an internal variable called `rules` within Knockout Validation. – Evan Hahn Mar 12 '13 at 01:33
  • NuGet gives me no updates, how come? Should I just copy paste the file from https://github.com/ericmbarnard/Knockout-Validation/blob/master/Src/knockout.validation.js ? – Display Name Mar 12 '13 at 01:36
  • I kinda figured it was internal `rules` that they use, but then how can they release this kind of code that throws things on innocent users? :) Thank you very much Evan! – Display Name Mar 12 '13 at 01:40
  • It's always annoying when a library has bugs, but code is complex! – Evan Hahn Mar 12 '13 at 01:41
  • The latest version of ko.validation doesn't have the fix, did it get lost through some integrations? – MBen Nov 20 '13 at 08:35
  • 1
    @MBen [Looks like it's still there...](https://github.com/Knockout-Contrib/Knockout-Validation/commit/1a07f345f6fd97fb267b068680fffe4f4e1623f4) – Evan Hahn Nov 20 '13 at 16:26