Comparing version 0.0.14 to 0.0.16
@@ -81,2 +81,5 @@ 'use strict'; | ||
var validationState = existing.find(function (object) { | ||
return object.valid !== undefined || object.messages !== undefined; | ||
}) || {}; | ||
this.rules = existing.map(function (ex) { | ||
@@ -106,2 +109,4 @@ return ex.rules; | ||
this.waiting = 0; | ||
this.valid = validationState.valid; | ||
this.messages = validationState.messages; | ||
} | ||
@@ -214,2 +219,14 @@ | ||
}, { | ||
key: 'setState', | ||
value: function setState(valid, messages) { | ||
this.valid = !!valid; | ||
this.messages = messages; | ||
} | ||
}, { | ||
key: 'initializeState', | ||
value: function initializeState(valid, messages) { | ||
this.setState(valid, messages); | ||
return this; | ||
} | ||
}, { | ||
key: 'forceRequirement', | ||
@@ -216,0 +233,0 @@ value: function forceRequirement(func, failureMessage, generator, name) { |
@@ -71,2 +71,10 @@ 'use strict'; | ||
}, { | ||
key: 'isValidationStateSet', | ||
value: function isValidationStateSet(formName) { | ||
var form = this.getForm(formName); | ||
return _Object$keys(form).some(function (key) { | ||
return form[key].valid !== undefined; | ||
}); | ||
} | ||
}, { | ||
key: 'register', | ||
@@ -81,2 +89,5 @@ value: function register(name, config, callback) { | ||
this.onUpdated(name, callback); | ||
if (this.isValidationStateSet(name)) { | ||
this.runCallbacks(name); | ||
} | ||
} | ||
@@ -106,2 +117,10 @@ }, { | ||
}, { | ||
key: 'runCallbacks', | ||
value: function runCallbacks(name) { | ||
var result = this.getResult(name); | ||
this.getCallbacks(name).forEach(function (callback) { | ||
callback(result); | ||
}); | ||
} | ||
}, { | ||
key: 'runValidation', | ||
@@ -117,6 +136,3 @@ value: function runValidation(name, data) { | ||
callbackTimeout = setTimeout(function () { | ||
var result = _this2.getResult(name); | ||
_this2.getCallbacks(name).forEach(function (callback) { | ||
callback(result); | ||
}); | ||
_this2.runCallbacks(name); | ||
}, 100); | ||
@@ -146,2 +162,18 @@ }; | ||
}, { | ||
key: 'setValidationState', | ||
value: function setValidationState(name, data) { | ||
var form = this.getForm(name); | ||
_Object$keys(form).forEach(function (key) { | ||
if (data[key] === undefined) { | ||
return; | ||
} | ||
var _data$key = data[key]; | ||
var valid = _data$key.valid; | ||
var messages = _data$key.messages; | ||
form[key].setState(valid, messages); | ||
}); | ||
this.runCallbacks(name); | ||
} | ||
}, { | ||
key: 'rule', | ||
@@ -148,0 +180,0 @@ get: function get() { |
{ | ||
"name": "valour", | ||
"version": "0.0.14", | ||
"version": "0.0.16", | ||
"description": "Simple javascript validation for any application", | ||
@@ -11,2 +11,3 @@ "main": "lib/valour.js", | ||
}, | ||
"files": [ "lib" ], | ||
"keywords": [ | ||
@@ -13,0 +14,0 @@ "validation", |
@@ -89,1 +89,46 @@ Valour.js | ||
``` | ||
### Setting the validation state | ||
There may be times when you want to tell valour about the validity of your form. This may be on initial page load, or after some server-side validation has occurred. Whatever the case may be, 'setValidationState' is what you'll need to call. This little utility function takes | ||
in a form name and an object, then updates the form with the information the object holds. Afterwards, it will run any callbacks you have given it to alert them of the new state. | ||
```javascript | ||
var valour = require('valour'); | ||
var result; | ||
valour.register('formName', { | ||
'email': valour.rule.isEmail() | ||
}, function (res) { | ||
result = res; | ||
}); | ||
valour.setValidationState('formName', { email: { valid: false } }); | ||
// result === { 'email': { valid: false } } | ||
valour.setValidationState('formName', { email: { valid: false, messages: ['New error.'] } }); | ||
// result === { 'email': { valid: false, messages: ['New error.'] } } | ||
valour.setValidationState('formName', { email: { valid: true, messages: ['All clear'] } }); | ||
// result === { 'email': { valid: true, messages: ['All clear'] } } | ||
``` | ||
Another way to do this is to initialize the state when registering. The callback provided will be called immediately, in this case. | ||
```javascript | ||
var valour = require('valour'); | ||
var result; | ||
valour.register('formName', { | ||
'email': valour.rule.isEmail().initializeState({ valid: false }) | ||
}, function (res) { | ||
result = res; | ||
}); | ||
// result === { 'email': { valid: false } } | ||
valour.register('anotherForm', { | ||
'email': valour.rule.isEmail().initializeState({ valid: true, messages: ['Some message'] }) | ||
}, function (res) { | ||
result = res; | ||
}); | ||
// result === { 'email': { valid: true, messages: ['Some message'] } } | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
133
36475
6
750
1