简体   繁体   English

立即使用计算出的可观察触发器在下拉菜单上进行敲除验证

[英]Knockout validation on a dropdown using a computed observable triggers immediately

I have a problem very similar to Knockout Validation evaluates immediately on load only the solution presented there doesn't work for me, because I cannot initialize the observable with "" as it is a computed observable with logic in it. 我有一个非常类似于Knockout验证的问题只有在解决方案对我不起作用的情况下, 立即在加载时立即进行验证评估 ,因为我无法使用“”初始化可观察对象,因为它是其中包含逻辑的计算可观察对象。

I also tried binding it to the dropdown with: 我还尝试通过以下方式将其绑定到下拉列表:

value: computedObservable()

or: 要么:

value: function(){computedObservable}

or: 要么:

value: function(){computedObservable}()

instead of: 代替:

value: computedObservable

The observable itself is defined as: 可观察对象本身定义为:

viewModel.computedObservable = ko.pureComputed({
    write: function (value) {
        viewModel.observable(value);
    },
    read: function () {
        return viewModel.isObservableComputed() ? viewModel.existingModel().observable() : viewModel.observable();
    }
}).extend({ required: true });

How do I prevent validation on load (it is shown in a bootstrap modal) yet still trigger the validation on a save? 如何防止在加载时进行验证(显示在引导程序模式中)却仍在保存时触发验证?

My current solution was to use conditional validation and set it to false initially and check another (required) field in the onlyIf, so this field only becomes required when the other has a value. 我当前的解决方案是使用条件验证,并将其初始设置为false并检查onlyIf中的另一个(必填)字段,因此仅当另一个具有值时才需要此字段。

Better solutions are still very much welcome. 更好的解决方案仍然非常受欢迎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM