ko.validation.group未在嵌套的observableArray上检测到我的错误 [英] ko.validation.group did not detect my errors on my nested observableArray
问题描述
我使用密集的Knockout绑定与Durandal/Breeze一起开发了一个项目.
I develop a project with Durandal/Breeze using intensive Knockout bindings.
我有一个视图,该视图使用以下可观察的图像:
I have a view which is using the following observable:
packing
- description
- weight
- ...
- isotopes
- name
- activity
- ...
正如您在上面看到的那样:我的packing
observable在内部包含一个isotopes
observableArray.这个packing
对象通过轻而易举的查询填充.
As you can see above: my packing
observable contains an isotopes
observableArray inside. This packing
object is filled from breeze with a query.
var query = entityQuery.from('Packings')
.where('id', '==', packingId)
.expand('isotopes');
我尝试使用 ko.validation 为所有这些设置适当的验证.
I try to set validation in place for all of this using ko.validation.
-
我的
packing
的description
属性是必需的
我的isotopes
observableArray的name
属性是必需的
The name
property of my isotopes
observableArray is required
我已成功验证说明.因此,每当用户清除绑定到description
的输入字段时,该字段就会以红色突出显示.每当用户添加一个新的空实体(微风)并单击保存时,该实体就会以红色突出显示.
I successfully validate on description. So whenever user clear the input field binded to description
, this one is highlighted in red. And whenever user add a new empty entity (breeze) and click on save, this one is highlighted in red.
这要感谢保存"按钮中的以下代码:
This works thanks to this code in the save button:
var validationErrorsCount = ko.computed(function () {
if (typeof packing() == 'undefined') return;
var validationErrors = ko.validation.group(packing());
return validationErrors().length;
})
if (validationErrorsCount() > 0) {
logError('Validation failed, please check.', '', true);
ko.validation.group(packing()).showAllMessages();
return;
}
到目前为止一切都很好.
So far so good.
现在,我需要为我的isotopes
observableArray验证属性name
.因此,每当用户清除绑定到name
的输入字段时,该字段就会以红色突出显示.有用.但是问题是,每当用户添加类型为isotope
的新的空实体(微风)时,都不要在name
输入框中键入任何内容,然后单击保存",该对象将不会在其中突出显示红色.
Now I need to validate the property name
for my isotopes
observableArray. So whenever user clear the input field binded to name
, this one is highlighted in red. It works. But the problem is whenever user add a new empty entity (breeze) of type isotope
, don't type anything for the name
input box and click on save, this one is not highlighted in red.
当我调试和检查值时,我可以清楚地看到:
When I debug and inspect values I can clearly see that:
-
ko.validation.group(packing(), {deep:false});
没有返回任何无效的东西
ko.validation.group(packing(), {deep:false});
did not return any invalid things
ko.validation.group(packing().isotopes(), {deep:false});
没有返回任何无效的东西
ko.validation.group(packing().isotopes(), {deep:false});
did not return any invalid things
所以看来ko.validation没有检测到我的无效输入.
So it seems that ko.validation did not detect my invalid inputs.
我的问题:当我在其中添加新元素时,如何验证嵌套的isotopes
observableArray?
My question: how to validate my nested isotopes
observableArray when I add a new element inside?
谢谢.
更新
Here is another SO post with the problem: Breeze.js & Knockout.js: translating breeze validation to knockout validation causes an 'Out of stack space' or 'Too much recursion'
这是我的肮脏"&临时破解(从第4行到第9行)
Here is my 'dirty' & temporary hack (from line 4 to 9)
// Check errors on the packing entity
var validationErrorsCount = ko.validation.group(packing()).length;
// Check errors on the isotopes entities !! code below is a temporary hack
ko.utils.arrayForEach(packing().isotopes(), function (isotope) {
if (!isotope.name.isValid()) {
validationErrorsCount = validationErrorsCount + 1;
ko.validation.group(isotope).showAllMessages();
}
});
if (validationErrorsCount > 0) {
logError('Validation failed, please check.', '', true);
ko.validation.group(packing()).showAllMessages();
return;
}
仍在等待一种更好的方法来验证我的内部(嵌套)模型.
Still waiting for a better way for validating my inner (nested) models.
推荐答案
如果更改,该怎么办:ko.validation.group(packing().isotopes(), { deep:false });
What if you change: ko.validation.group(packing().isotopes(), { deep:false });
至:ko.validation.group(packing().isotopes(), { deep:true });
这不是重点吗?
这篇关于ko.validation.group未在嵌套的observableArray上检测到我的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!