Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove 'validatesOn' configuration.

  • Loading branch information...
commit 91fc25a89fcc67f3a43ff7952d8478f246442a06 1 parent e3ddc01
Luiz Ribeiro luizfar authored
Showing with 3 additions and 66 deletions.
  1. +0 −33 spec/observable-spec.js
  2. +3 −33 src/ko-validation.js
33 spec/observable-spec.js
View
@@ -36,39 +36,6 @@ describe('observables validation', function () {
expect(observable.isValid()).toBe(false);
});
- describe('when the observable is to be validated only on input changes', function () {
- var observable, viewModel;
-
- beforeEach(function () {
- observable = ko.observable('Value').extend({
- required: ['Value is required'],
- validatesOn: 'inputChange'
- });
-
- viewModel = { obs: observable };
-
- setFixtures('<div id="parent"><input id="input" data-bind="value: obs"/></div>');
- ko.applyBindings(viewModel, $('#parent')[0]);
- });
-
- it('does not validate when the observable changes', function () {
- expect(observable.isValid()).toBe(true);
-
- observable('');
-
- expect(observable.isValid()).toBe(true);
- });
-
- it('validates when the input changes', function () {
- expect(observable.isValid()).toBe(true);
-
- $('#input').val('').trigger('change');
-
- expect(observable.isValid()).toBe(false);
- expect(observable.validationMessage()).toBe('Value is required');
- });
- });
-
describe('validating an input', function () {
var observable, viewModel;
36 src/ko-validation.js
View
@@ -105,12 +105,9 @@ ko.validation.registerValidator = function (name, validatorFactory) {
observable.isValid = ko.computed(function () {
return observable.validationState() !== ko.validation.validationStates.INVALID;
});
- if (observable.__validatesOn__ !== 'inputChange') {
- observable.__validatesOn__ = 'change';
- observable.__validationSubscription__ = observable.subscribe(function () {
- ko.validation.utils.runValidations(observable);
- });
- }
+ observable.subscribe(function () {
+ ko.validation.utils.runValidations(observable);
+ });
}
validator = ko.validation.utils.createValidator(name, param);
@@ -132,19 +129,6 @@ ko.validation.registerValidator = function (name, validatorFactory) {
return validationElement;
}
- function bindEventListenerToRunValidation(element, observableToValidate) {
- var elementType, eventName;
-
- elementType = element.getAttribute('type');
- eventName = (elementType && elementType.toLowerCase() === 'checkbox') ? 'click' : 'change';
-
- ko.utils.registerEventHandler(element, eventName, function () {
- if (ko.validation.utils.hasValidators(observableToValidate)) {
- ko.validation.utils.runValidations(observableToValidate);
- }
- });
- }
-
function updateValidationMessage(element, observable) {
ko.bindingHandlers.visible.update(element, function () {
return observable.validationState() !== ko.validation.validationStates.PRISTINE;
@@ -159,9 +143,6 @@ ko.validation.registerValidator = function (name, validatorFactory) {
}
function initValidationFor(inputElement, observable) {
- if (observable.__validatesOn__ === 'inputChange') {
- bindEventListenerToRunValidation(inputElement, observable);
- }
var messageSubscription = observable.validationState.subscribe(function () {
if (observable.__hasCustomValidationElement__) {
messageSubscription.dispose();
@@ -176,17 +157,6 @@ ko.validation.registerValidator = function (name, validatorFactory) {
});
}
- ko.extenders.validatesOn = function (observable, eventName) {
- if (eventName !== 'change' && eventName !== 'inputChange') {
- throw new Error('Observable can be validated only on events "change" or "inputChange".');
- }
- observable.__validatesOn__ = eventName;
- if (eventName === 'inputChange' && observable.__validationSubscription__) {
- observable.__validationSubscription__.dispose();
- }
- return observable;
- };
-
ko.extenders.validatesAfter = function (observable, dependentObservables) {
ko.utils.arrayForEach(dependentObservables, function (dependentObservable) {
dependentObservable.__validates__ = dependentObservable.__validates__ || [];
Please sign in to comment.
Something went wrong with that request. Please try again.