简体   繁体   中英

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.

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.

Better solutions are still very much welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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