I'm trying to set up a validation group to display errors after a button's pressed.
My issue is I can't get the validation group to update correctly each time the validation error changes.
In my example, I can see the the .errors property of the field observable update correctly each time I leave the text box (intended). For the validation group, the documentation points to calling .showAllMessages() or evaluating the group itself.
I've created an example below. A summary of the behaviour I see:
- On load, the 'required' validation message shows (ok)
- Whenever the validation error changes on the field, validation group doesn't update (grrrr)
- Once the field becomes completely valid, the validation group will update (ok)
- Once the field becomes invalid after being valid, it will update with the first validation message, but go not update properly as above.
In the example, I've set up these validations on a single field:
- required
- minLength: 3
- maxLength: 10
- numeric
The view:
<label>Test number</label>
<input type="text" data-bind="value: myNumber"/>
<div>myNumber error: <span data-bind="text: myNumber.errors"/></div>
<div>Validation group:
<ul data-bind="foreach: validationGroup"><li data-bind="text: $data"></li></ul>
</div>
<button type="submit" class="btn" data-bind="click: testValidate">Update validation group</button>
The view model:
var viewModel = {
myNumber: ko.observable().extend({
maxLength: 10,
number: {
message: "Please ensure that myNumber contains only numeric characters"
},
required: {
message: "myNumber is required"
},
minLength: 3
})
};
viewModel.validationGroup = ko.validation.group([viewModel.myNumber]);
viewModel.testValidate = function () {
// Try both, to be sure...
viewModel.validationGroup.showAllMessages();
viewModel.validationGroup();
};