Validate observables within a collection using Knockout Validation

孤者浪人 提交于 2019-12-24 13:19:28

问题


I have a message field and collection of people being bound in an unordered list accompanied each with a drop down list of colors and a default selection.

Validation on the message field works, erase the text and an error is displayed and the error count increases. However, when you change one of the drop down entries to "Choose.." (effectively unselecting the option), you receive a "this field is required" message correctly however the errors object does not appear to be updating. It must obviously have something to do with the fact it's a collection? See what I mean when you press the submit button or view the span data binding.

I also need validation to kick in on dynamically added rows, so when you press 'add another row' I need the errors object to increase on that too.

http://jsfiddle.net/goneale/TJGS3/

Could anybody take a look at this and help me out at all?


回答1:


Update: With latest version of validation library you can use the live: true option to get it to listen to observableArrays

grouping: { deep: true, observable: true, live: true }

http://jsfiddle.net/fYrbt/29/

Old answer:

Sadly the group does not get reevaluated when a new item is inserted. Subscribe to the array and do it explicit.

this.items.subscribe(function() {
     this.errors = ko.validation.group(this);
     this.errors.showAllMessages();
}, this);

http://jsfiddle.net/fYrbt/



来源:https://stackoverflow.com/questions/16229775/validate-observables-within-a-collection-using-knockout-validation

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