Created
March 9, 2011 17:31
-
-
Save JamieMason/862601 to your computer and use it in GitHub Desktop.
First attempt at proposal for the syntax for registration validators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Constructor | |
function Validator (oParams, oProtected) | |
{ | |
// optionally share private stuff between other validators | |
oProtected = oProtected || {}; | |
// ... | |
} | |
// Instantiate | |
var registrar = new Validator ({ | |
name: 'registrar', | |
fields: { | |
telephone : { | |
minlength : { | |
// required | |
isValid : function(){ return this.length > 5; }, | |
failMsg : 'Telephone number must be a minimum of 6 characters', | |
// below are optional/overrides | |
passMsg : 'Thanks, Telephone number accepted', | |
required : true, // default is false | |
defaultValue : '123456', // default is null | |
// default format 'validator:<validator name>.<field name>.<requirement name>.<valid|invalid> | |
passEvent : 'validator:registrar.telephone.minlength.valid', | |
failEvent : 'validator:registrar.telephone.minlength.invalid' | |
}, | |
validchars : { | |
isValid : function(){ return this.search(/[^0-9\-]/gi) === -1; }, | |
failMsg : 'Telephone must only contain numbers or dashes' | |
}, | |
someOtherTestCase : { | |
isValid : function(){ return /* Boolean */; }, | |
failMsg : 'etc...' | |
} | |
}, | |
someOtherField : { | |
someTestCase : { | |
isValid : function(){ return /* Boolean */; }, | |
failMsg : 'etc...' | |
} | |
} | |
} | |
}); | |
// get field value (returns value) | |
registrar.field('firstName'); | |
// Set field value (returns boolean for valid or not, events fire in background) | |
registrar.field('firstName', 'Steve'); | |
// Get all values (returns object) | |
registrar.field(); | |
// Set multiple values (returns boolean for if ALL were valid or not, events fire in background) | |
registrar.field({ | |
firstName: 'Steve', | |
surname: 'Melrose' | |
}); | |
// Get array of Strings of names of unfinished fields | |
registrar.incomplete(); | |
// Get array of Objects of invalid fields and their failing tests | |
registrar.invalid(); | |
// Get array of Strings of names of required fields | |
registrar.required(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instantiation example with MessageBroker | |
var registrar = new Validator ({ | |
name: 'registrar', | |
fields: { | |
telephone : { | |
validchars : { | |
isValid : function(){ return this.search(/[^0-9\-]/gi) === -1; }, | |
failMsg : MessageBroker.E_WHATEVER_IT_IS | |
}, | |
someOtherTestCase : { | |
isValid : function(){ return /* Boolean */; }, | |
failMsg : MessageBroker.E_WHATEVER_IT_IS | |
} | |
}, | |
someOtherField : { | |
someTestCase : { | |
isValid : function(){ return /* Boolean */; }, | |
failMsg : MessageBroker.E_WHATEVER_IT_IS | |
} | |
} | |
} | |
}); |
Author
JamieMason
commented
Mar 9, 2011
- When all of a field's tests pass, validator:{validator name}.{field name}.valid will fire.
- When any of a field's tests fail validator:{validator name}.{field name}.invalid will fire.
- When any of a validator's field's tests fail, validator:{validator name}.invalid will fire.
- When all of a validator's field's tests pass, validator:{validator name}.valid will fire.
- When none of a validator's required field values are null or invalid AND no optional fields are invalid, validator:{validator name}.complete will fire.
- When a previously complete validator no longer passes the above case, validator:{validator name}.incomplete will fire.
To do...
- How to define and handle asynchronous tests
- How/if we need to handle compound fields such as date of birth here, or in the project's js
- How to manage the utils is _isValidDate, _getStringLengthWithoutWhitespace etc.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment