问题
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:
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?
回答1:
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.
来源:https://stackoverflow.com/questions/15351041/knockout-validation-error-in-internet-explorer