Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime-instantiated nested model not validating #182

Closed
adamstrickland opened this issue Nov 30, 2012 · 3 comments
Closed

Runtime-instantiated nested model not validating #182

adamstrickland opened this issue Nov 30, 2012 · 3 comments

Comments

@adamstrickland
Copy link

May be related to https://github.com/ericmbarnard/Knockout-Validation/issues/99.

I am using a ViewModel with a nested underlying observable Model, both of which are instantiated at runtime before bindings are applied. In short, the underlying model, if ever found to be invalid, will never validate correctly. I'm using ko.validation.group to perform the validation (perhaps this is a bad practice?), invoked from the ViewModel on the Model. The result's errors array includes a first-level error of null; the observable that generates this is the Model itself.

I found that by changing if (!observable.isValid()) { to if (!observable.isValid() && observable.error) { (lines /Src/knockout.validation.js:250,263) works around the issue, but I'm not certain this is the right approach. It's also possible I should be doing something differently in the first place (such as not using ko.validation.group directly).

I have created (and will attach) a test to demonstrate, as well as a patch for knockout.validation.js.

An additional thing I noticed while building the test is that the underlying model never appears to be a validatable (uncommenting // ok(vm._model.isValid, 'Underlying model should be validatable'); in the test will cause it to fail). Again, am unsure if this is relevant or important.

@stalniy
Copy link

stalniy commented Oct 26, 2013

Actually there is a more simple solution to fix this issue - just re-use the same viewmodel don't create new one each time:

function Parent() {
  this.child = new Child();
}

Parent.prototype.load = function (data)  {
  this.child.update(data);
};

function Child() {
  this.firstName.extend({ required: true });
}

Child.prototype.update = function (data) {
  for (var prop in data) {
    if (data.hasOwnProperty(prop) && ko.isObservable(this[prop])) {
      this[prop](data);
    }
  }
};

var vm = new Parent();

vm.load({ firstName: 'firstName' });

@crissdev
Copy link
Member

Closing this issue in favor of #398

@maksimbykov
Copy link

I had same problem, nested observables with validation. So the one magic:
in self.errors = ko.validation.group(self.submissionAnswers, { deep: true, live: true });
pay attention on special additional param: object that contains field live: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants