Skip to content

Instantly share code, notes, and snippets.

@bettdouglas
Created June 19, 2019 12:41
Show Gist options
  • Save bettdouglas/47ffc42d8ba1c977da07bbb5bae33fc6 to your computer and use it in GitHub Desktop.
Save bettdouglas/47ffc42d8ba1c977da07bbb5bae33fc6 to your computer and use it in GitHub Desktop.
class Validator {
String text; ///the text to validate. This will be the controller.text value
TypesValidator _validator; //the validation object where we'll define our rules on
DataType type; ///the object that will do the validation for us
Validator(this.text,this.type){
_validator = new TypesValidator(text,type); //initialise the object to do validation
this.create(); //define the rules to our validator object which will be a singleton that will handle multi-validation
}
TypesValidator get validator => _validator; //a get to access the validation object
TypesValidator create(){
_validator.ruleFor('name', ()=>text)
..notEmpty()
..withMessage('Please enter a name')
..length(2, 30)
..withMessage('Please enter a reasonable name');
_validator.ruleFor('email', ()=>text)
..notEmpty()
..withMessage('Email cannot be empty')
..emailAddress()
..withMessage('Enter a valid email');
_validator.ruleFor('text', ()=>text)
..notEmpty()
..withMessage('Cannot be empty');
_validator.ruleFor('phone', ()=>text)
..notEmpty()
..withMessage('Phone number cannot be empty')
..matches('[0-9]{0,14}\$')
..withMessage('Phone number doesn\'t seem correct');
_validator.ruleFor('int', ()=>text)
..matches('(?<=\\s|^)[-+]?\\d+(?=\\s|\$)')
..withMessage('Enter a valid number');
return _validator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment