| OLD | NEW |
| (Empty) | |
| 1 part of angular.directive; |
| 2 |
| 3 /** |
| 4 * _NgModelValidator refers to the required super-class which is used when creat
ing |
| 5 * validation services that are used with [ngModel]. It is expected that any chi
ld-classes |
| 6 * that inherit from this perform the necessary logic to return a simple true/fa
lse response |
| 7 * when validating the contents of the model data. |
| 8 */ |
| 9 abstract class _NgModelValidator { |
| 10 final dom.Element inputElement; |
| 11 final NgModel ngModel; |
| 12 final Scope scope; |
| 13 bool _listening = false; |
| 14 |
| 15 _NgModelValidator(dom.Element this.inputElement, NgModel this.ngModel, Scope t
his.scope); |
| 16 |
| 17 /** |
| 18 * Registers the validator with to attached model. |
| 19 */ |
| 20 bool listen() { |
| 21 if(!_listening) { |
| 22 _listening = true; |
| 23 this.ngModel.addValidator(this); |
| 24 } |
| 25 } |
| 26 |
| 27 get value => ngModel.viewValue; |
| 28 |
| 29 /** |
| 30 * De-registers the validator with to attached model. |
| 31 */ |
| 32 bool unlisten() { |
| 33 if(_listening) { |
| 34 _listening = false; |
| 35 this.ngModel.removeValidator(this); |
| 36 } |
| 37 } |
| 38 |
| 39 /** |
| 40 * Returns true/false depending on the status of the validator's validation me
chanism |
| 41 */ |
| 42 bool isValid(); |
| 43 } |
| 44 |
| 45 /** |
| 46 * Validates the model depending if required or ng-required is present on the el
ement. |
| 47 */ |
| 48 @NgDirective(selector: '[ng-model][required]') |
| 49 @NgDirective(selector: '[ng-model][ng-required]', map: const {'ng-required': '=>
required'}) |
| 50 class NgModelRequiredValidator extends _NgModelValidator { |
| 51 bool _required; |
| 52 get name => 'required'; |
| 53 |
| 54 NgModelRequiredValidator(dom.Element inputElement, NgModel ngModel, Scope scop
e, NodeAttrs attrs): |
| 55 super(inputElement, ngModel, scope) { |
| 56 if(attrs['required'] != null) { |
| 57 required = true; |
| 58 } |
| 59 } |
| 60 |
| 61 bool isValid() { |
| 62 return !required || (value != null && value.length > 0); |
| 63 } |
| 64 |
| 65 @NgAttr('required') |
| 66 get required => _required; |
| 67 set required(value) { |
| 68 if(value is String) return; |
| 69 (_required = value) == true ? listen() : unlisten(); |
| 70 } |
| 71 } |
| 72 |
| 73 /** |
| 74 * Validates the model to see if its contents match a valid URL pattern. |
| 75 */ |
| 76 @NgDirective(selector: 'input[type=url][ng-model]') |
| 77 class NgModelUrlValidator extends _NgModelValidator { |
| 78 static final URL_REGEXP = new RegExp( |
| 79 r'^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?' + |
| 80 r'(\/|\/([\w#!:.?+=&%@!\-\/]))?$'); |
| 81 |
| 82 get name => 'url'; |
| 83 |
| 84 NgModelUrlValidator(dom.Element inputElement, NgModel ngModel, Scope scope): |
| 85 super(inputElement, ngModel, scope) { |
| 86 listen(); |
| 87 } |
| 88 |
| 89 bool isValid() { |
| 90 return value == null || value.length == 0 || URL_REGEXP.hasMatch(value); |
| 91 } |
| 92 } |
| 93 |
| 94 /** |
| 95 * Validates the model to see if its contents match a valid email pattern. |
| 96 */ |
| 97 @NgDirective(selector: 'input[type=email][ng-model]') |
| 98 class NgModelEmailValidator extends _NgModelValidator { |
| 99 static final EMAIL_REGEXP = new RegExp( |
| 100 r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$'); |
| 101 |
| 102 get name => 'email'; |
| 103 |
| 104 NgModelEmailValidator(dom.Element inputElement, NgModel ngModel, Scope scope): |
| 105 super(inputElement, ngModel, scope) { |
| 106 listen(); |
| 107 } |
| 108 |
| 109 bool isValid() { |
| 110 return value == null || value.length == 0 || EMAIL_REGEXP.hasMatch(value); |
| 111 } |
| 112 } |
| 113 |
| 114 /** |
| 115 * Validates the model to see if its contents match a valid number. |
| 116 */ |
| 117 @NgDirective(selector: 'input[type=number][ng-model]') |
| 118 class NgModelNumberValidator extends _NgModelValidator { |
| 119 get name => 'number'; |
| 120 |
| 121 NgModelNumberValidator(dom.Element inputElement, NgModel ngModel, Scope scope)
: |
| 122 super(inputElement, ngModel, scope) { |
| 123 listen(); |
| 124 } |
| 125 |
| 126 bool isValid() { |
| 127 if(value != null) { |
| 128 try { |
| 129 num val = double.parse(value.toString()); |
| 130 } catch(exception, stackTrace) { |
| 131 return false; |
| 132 } |
| 133 } |
| 134 return true; |
| 135 } |
| 136 } |
| 137 |
| 138 /** |
| 139 * Validates the model to see if its contents match the given pattern present on
either the |
| 140 * HTML pattern or ng-pattern attributes present on the input element. |
| 141 */ |
| 142 @NgDirective(selector: '[ng-model][pattern]') |
| 143 @NgDirective(selector: '[ng-model][ng-pattern]', map: const {'ng-pattern': '=>pa
ttern'}) |
| 144 class NgModelPatternValidator extends _NgModelValidator { |
| 145 RegExp _pattern; |
| 146 |
| 147 get name => 'pattern'; |
| 148 |
| 149 NgModelPatternValidator(dom.Element inputElement, NgModel ngModel, Scope scope
): |
| 150 super(inputElement, ngModel, scope) { |
| 151 listen(); |
| 152 } |
| 153 |
| 154 bool isValid() { |
| 155 if(_pattern != null && value != null && value.length > 0) { |
| 156 return _pattern.hasMatch(ngModel.viewValue); |
| 157 } |
| 158 |
| 159 //remember, only required validates for the input being empty |
| 160 return true; |
| 161 } |
| 162 |
| 163 @NgAttr('pattern') |
| 164 get pattern => _pattern; |
| 165 set pattern(val) { |
| 166 if(val != null && val.length > 0) { |
| 167 _pattern = new RegExp(val); |
| 168 listen(); |
| 169 } else { |
| 170 _pattern = null; |
| 171 unlisten(); |
| 172 } |
| 173 } |
| 174 } |
| 175 |
| 176 /** |
| 177 * Validates the model to see if the length of its contents are greater than or
equal to the minimum length |
| 178 * set in place by the HTML minlength or ng-minlength attributes present on the
input element. |
| 179 */ |
| 180 @NgDirective(selector: '[ng-model][minlength]') |
| 181 @NgDirective(selector: '[ng-model][ng-minlength]', map: const {'ng-minlength': '
=>minlength'}) |
| 182 class NgModelMinLengthValidator extends _NgModelValidator { |
| 183 int _minlength; |
| 184 |
| 185 get name => 'minlength'; |
| 186 |
| 187 NgModelMinLengthValidator(dom.Element inputElement, NgModel ngModel, Scope sco
pe): |
| 188 super(inputElement, ngModel, scope) { |
| 189 listen(); |
| 190 } |
| 191 |
| 192 bool isValid() { |
| 193 //remember, only required validates for the input being empty |
| 194 if(_minlength == 0 || value == null || value.length == 0) { |
| 195 return true; |
| 196 } |
| 197 return value.length >= _minlength; |
| 198 } |
| 199 |
| 200 @NgAttr('minlength') |
| 201 get minlength => _minlength; |
| 202 set minlength(value) { |
| 203 _minlength = value == null ? 0 : int.parse(value.toString()); |
| 204 } |
| 205 } |
| 206 |
| 207 /** |
| 208 * Validates the model to see if the length of its contents are less than or equ
al to the maximum length |
| 209 * set in place by the HTML maxlength or ng-maxlength attributes present on the
input element. |
| 210 */ |
| 211 @NgDirective(selector: '[ng-model][maxlength]') |
| 212 @NgDirective(selector: '[ng-model][ng-maxlength]', map: const {'ng-maxlength': '
=>maxlength'}) |
| 213 class NgModelMaxLengthValidator extends _NgModelValidator { |
| 214 int _maxlength = 0; |
| 215 |
| 216 get name => 'maxlength'; |
| 217 |
| 218 NgModelMaxLengthValidator(dom.Element inputElement, NgModel ngModel, Scope sco
pe): |
| 219 super(inputElement, ngModel, scope) { |
| 220 listen(); |
| 221 } |
| 222 |
| 223 bool isValid() { |
| 224 return _maxlength == 0 || (value == null ? 0 : value.length) <= _maxlength; |
| 225 } |
| 226 |
| 227 @NgAttr('maxlength') |
| 228 get maxlength => _maxlength; |
| 229 set maxlength(value) { |
| 230 int length = value == null ? 0 : int.parse(value.toString()); |
| 231 if(length != maxlength) { |
| 232 _maxlength = length; |
| 233 } |
| 234 } |
| 235 } |
| OLD | NEW |