Knockout Validation evaluates immediately on load

五迷三道 提交于 2019-11-29 09:09:12
Velmurugan

After applying the bindings for the viewmodel. Then for that viewmodel make showAllMessages as false

Example

YourViewmodelname.errors.showAllMessages(false);

Quoting KO page.... ( http://knockoutjs.com/documentation/options-binding.html )

KO will prefix the list of items with one that displays the text “Select an item…” and has the value undefined. So, if myChosenValue holds the value undefined (which observables do by default), then the dummy option will be selected. If the optionsCaption parameter is an observable, then the text of the initial item will update as the observable’s value changes.

So, I solved it by setting "undefined" when defining the property, see example below:

self.myProperty = ko.observable(undefined).extend({
    required :  {"Field Required"}
});

Hope this helps...

I figured out this issue on my own.

The problem exists between the razor engine templating the select options, and then later binding the value of the selected element to Knockout.

Despite the fact that there is no user-inputted value in the select box, the default value, the "--select--" actually contains a value. In my case it was an empty string. Therefore, when I applied the knockout bindings, my viewmodel property was "updated" with the empty string value, and therefore validation fired.

To get around this in my case I set the default value of my model to be an empty string. Therefore when the bindings are applied, there is no valueHasMutated event fired on the Knockout observable, and thus no validation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!