Created
June 19, 2019 12:41
-
-
Save bettdouglas/47ffc42d8ba1c977da07bbb5bae33fc6 to your computer and use it in GitHub Desktop.
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
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