0

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?

Community
  • 1
  • 1
Ytrog
  • 170
  • 2
  • 15
  • Does `ko.validation.init({ messagesOnModified: true })` help out? – user3297291 Jul 11 '16 at 11:15
  • @user3297291 no, while this was set to false (default = true), setting it to true didn't help :( – Ytrog Jul 11 '16 at 12:06
  • I tried to reproduce your issue, but for me the validation with the option works as intended. https://jsfiddle.net/5r1u154z/ Can you update this fiddle to show what happens in your situation? – user3297291 Jul 11 '16 at 12:58
  • @user3297291 I think this would be close: https://jsfiddle.net/5r1u154z/1/ – Ytrog Jul 11 '16 at 13:24
  • Still seems to work? https://jsfiddle.net/5r1u154z/2/ (Did you try switching between the two option objects?) – user3297291 Jul 11 '16 at 13:43
  • @user3297291 I see that you changed the messagesOnModified property. However outside of the fiddle it doesn't work. My temporary fix is checking in onlyIf if another (required) field is empty or not. – Ytrog Jul 11 '16 at 13:48
  • It's hard for me to help you out if you can't somehow reproduce your issue. The fiddle I provided shows how the option that is specifically designed to solve these kind of problems works. If it doesn't for you, there's probably something else going on in code you haven't shown. I'd suggest you either refactor some stuff in your own code until it works as the example, or include a _non-working repro_ in your question. – user3297291 Jul 11 '16 at 13:51
  • @user3297291 I would if I knew what other factors come into play here. It is a complex project. – Ytrog Jul 12 '16 at 08:06

1 Answers1

0

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.

Ytrog
  • 170
  • 2
  • 15