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 | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To do...