0
votes

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?

1
Does ko.validation.init({ messagesOnModified: true }) help out? - user3297291
@user3297291 no, while this was set to false (default = true), setting it to true didn't help :( - Ytrog
I tried to reproduce your issue, but for me the validation with the option works as intended. jsfiddle.net/5r1u154z Can you update this fiddle to show what happens in your situation? - user3297291
@user3297291 I think this would be close: jsfiddle.net/5r1u154z/1 - Ytrog
Still seems to work? jsfiddle.net/5r1u154z/2 (Did you try switching between the two option objects?) - user3297291

1 Answers

0
votes

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.