使用敲除验证来验证集合中的可观察对象 [英] Validate observables within a collection using Knockout Validation

查看:65
本文介绍了使用敲除验证来验证集合中的可观察对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个消息字段和people的集合绑定在一个无序列表中,每一个都带有一个下拉颜色列表和一个默认选择.

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.

消息字段上的验证有效,删除文本并显示错误,并且错误计数增加.但是,当您将下拉菜单项之一更改为"Choose .."(有效地取消选择该选项)时,您会正确地收到此字段是必需的"消息,但是errors对象似乎没有更新.很明显,这一定与它是一个集合有关吗?按下提交按钮或查看跨度数据绑定时,请明白我的意思.

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.

我还需要验证才能插入动态添加的行,因此当您按添加另一行"时,我也需要errors对象来增加该行.

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?

推荐答案

更新: 使用最新版本的验证库,您可以使用live:true选项使它侦听observableArrays

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/

旧答案:

可悲的是,当插入新项目时,该组不会得到重新评估.订阅该数组并进行显式操作.

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/

这篇关于使用敲除验证来验证集合中的可观察对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆