ericmbarnard/Knockout-Validation — Issue #32: How does isModified work?
Author: grofit
Created: 2012-03-01 15:31:06+00:00
State: open

Just before I get to the main question here is a bit of context and it may just be me being an idiot, but I have a case at the moment where I have a custom binding hooked up which subscribes the observable to ``IsValid`` changing:

```
/*globals ko $*/
var originalKoValueInit = ko.bindingHandlers.value.init;
ko.bindingHandlers.value.init = function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
originalKoValueInit(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);

var observable = valueAccessor();
if (!ko.validation.utils.isValidatable(observable)) { return; }

var $element = $(element);
observable.isValid.subscribe(function (valid) {
if (!valid) {
// Raise a validation error event here
}
else {
// Raise a validation success event here
}
});
}
```

Now this works great, whenever the observable is changed it will raise the relevant error. However I have a scenario where if someone doesn't use a field with validation on and submits, as it is not having its value changed it will not trigger any ``isValid`` subscriptions, but the errors from the ``group(someModel);`` will reflect the error with the field correctly, however in my specific scenario no errors would appear on the page (assuming the raise validation error displays some form of validation error) even though the form cannot submit because they need to sort out the errors.

With the above scenario I want any validation errors to be displayed when the users click on the submit button. I am basically doing ``if(errorsFromGroup().length == 0) { Progress(); } else { Fail(); }`` so I thought I could just put ``errorsFromGroup.showAllMessages()`` before the example ``Fail()`` outcome. However this doesn't seem to do anything in my scenario, I looked in the source code and saw that when you do ``showAllMessages`` it just loops through all observables and sets the ``isModified`` observable to ``true``, but I cannot see anything subscribing to this observable.

The thing that puzzles me is that within your example JSfiddle document, you use this ``showAllMessages`` to display all the inline validation errors on the page, so I was assuming in a round about way the ``isModified`` property influences some logic within the validation framework, but I just cannot find it anywhere... So am I missing some crucial part or is this observable not used anywhere, and some other voodoo magic is making your inline validation messages appear?

As far as I am concerned the main thing I want to do is get the isValid subscriber to fire whenever I press this imaginary submit button, is this possible? as there must be some crucial bit I am not seeing which hooks the observables value changing to trigger an isValid check, but for the life of me cannot see it...