Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1968)

Unified Diff: third_party/pkg/angular/lib/directive/ng_model_validators.dart

Issue 180873006: Update the Angular/DI tests to latest from github. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Review feedback Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/pkg/angular/lib/directive/ng_model_validators.dart
diff --git a/third_party/pkg/angular/lib/directive/ng_model_validators.dart b/third_party/pkg/angular/lib/directive/ng_model_validators.dart
index 9aa6ecfa0e2c3664bc1c89268113ecca038e5e9b..7fd5ba5db0e8d0aaaaee500785d16690c99f07f9 100644
--- a/third_party/pkg/angular/lib/directive/ng_model_validators.dart
+++ b/third_party/pkg/angular/lib/directive/ng_model_validators.dart
@@ -1,72 +1,40 @@
part of angular.directive;
-/**
- * _NgModelValidator refers to the required super-class which is used when creating
- * validation services that are used with [ngModel]. It is expected that any child-classes
- * that inherit from this perform the necessary logic to return a simple true/false response
- * when validating the contents of the model data.
- */
-abstract class _NgModelValidator {
- final dom.Element inputElement;
- final NgModel ngModel;
- final Scope scope;
- bool _listening = false;
-
- _NgModelValidator(dom.Element this.inputElement, NgModel this.ngModel, Scope this.scope);
-
- /**
- * Registers the validator with to attached model.
- */
- bool listen() {
- if(!_listening) {
- _listening = true;
- this.ngModel.addValidator(this);
- }
- }
-
- get value => ngModel.viewValue;
-
- /**
- * De-registers the validator with to attached model.
- */
- bool unlisten() {
- if(_listening) {
- _listening = false;
- this.ngModel.removeValidator(this);
- }
- }
-
- /**
- * Returns true/false depending on the status of the validator's validation mechanism
- */
- bool isValid();
+abstract class NgValidatable {
+ String get name;
+ bool isValid(value);
}
/**
* Validates the model depending if required or ng-required is present on the element.
*/
-@NgDirective(selector: '[ng-model][required]')
-@NgDirective(selector: '[ng-model][ng-required]', map: const {'ng-required': '=>required'})
-class NgModelRequiredValidator extends _NgModelValidator {
- bool _required;
- get name => 'required';
-
- NgModelRequiredValidator(dom.Element inputElement, NgModel ngModel, Scope scope, NodeAttrs attrs):
- super(inputElement, ngModel, scope) {
- if(attrs['required'] != null) {
- required = true;
- }
- }
+@NgDirective(
+ selector: '[ng-model][required]')
+@NgDirective(
+ selector: '[ng-model][ng-required]',
+ map: const {'ng-required': '=>required'})
+class NgModelRequiredValidator implements NgValidatable {
+ bool _required = true;
+
+ String get name => 'required';
+
+ NgModelRequiredValidator(NgModel ngModel) {
+ ngModel.addValidator(this);
+ }
- bool isValid() {
- return !required || (value != null && value.length > 0);
+ bool isValid(value) {
+ // Any element which isn't required is always valid.
+ if (!_required) return true;
+ // Null is not a value, therefore not valid.
+ if (value == null) return false;
+ // Empty lists and/or strings are not valid.
+ // NOTE: This is an excellent use case for structural typing.
+ // We really want anything object that has a 'isEmpty' property.
+ return !((value is List || value is String) && value.isEmpty);
}
- @NgAttr('required')
- get required => _required;
set required(value) {
- if(value is String) return;
- (_required = value) == true ? listen() : unlisten();
+ _required = value == null ? false : value;
}
}
@@ -74,57 +42,52 @@ class NgModelRequiredValidator extends _NgModelValidator {
* Validates the model to see if its contents match a valid URL pattern.
*/
@NgDirective(selector: 'input[type=url][ng-model]')
-class NgModelUrlValidator extends _NgModelValidator {
+class NgModelUrlValidator implements NgValidatable {
static final URL_REGEXP = new RegExp(
r'^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?' +
r'(\/|\/([\w#!:.?+=&%@!\-\/]))?$');
- get name => 'url';
-
- NgModelUrlValidator(dom.Element inputElement, NgModel ngModel, Scope scope):
- super(inputElement, ngModel, scope) {
- listen();
- }
+ String get name => 'url';
- bool isValid() {
- return value == null || value.length == 0 || URL_REGEXP.hasMatch(value);
+ NgModelUrlValidator(NgModel ngModel) {
+ ngModel.addValidator(this);
}
+
+ bool isValid(value) =>
+ value == null || value.isEmpty || URL_REGEXP.hasMatch(value);
}
/**
* Validates the model to see if its contents match a valid email pattern.
*/
@NgDirective(selector: 'input[type=email][ng-model]')
-class NgModelEmailValidator extends _NgModelValidator {
+class NgModelEmailValidator implements NgValidatable {
static final EMAIL_REGEXP = new RegExp(
r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$');
- get name => 'email';
-
- NgModelEmailValidator(dom.Element inputElement, NgModel ngModel, Scope scope):
- super(inputElement, ngModel, scope) {
- listen();
- }
+ String get name => 'email';
- bool isValid() {
- return value == null || value.length == 0 || EMAIL_REGEXP.hasMatch(value);
+ NgModelEmailValidator(NgModel ngModel) {
+ ngModel.addValidator(this);
}
+
+ bool isValid(value) =>
+ value == null || value.isEmpty || EMAIL_REGEXP.hasMatch(value);
}
/**
* Validates the model to see if its contents match a valid number.
*/
@NgDirective(selector: 'input[type=number][ng-model]')
-class NgModelNumberValidator extends _NgModelValidator {
- get name => 'number';
+class NgModelNumberValidator implements NgValidatable {
+ String get name => 'number';
- NgModelNumberValidator(dom.Element inputElement, NgModel ngModel, Scope scope):
- super(inputElement, ngModel, scope) {
- listen();
- }
+ NgModelNumberValidator(NgModel ngModel) {
+ ngModel.addValidator(this);
+ }
- bool isValid() {
- if(value != null) {
+ bool isValid(value) {
+ if (value != null) {
try {
num val = double.parse(value.toString());
} catch(exception, stackTrace) {
@@ -140,96 +103,80 @@ class NgModelNumberValidator extends _NgModelValidator {
* HTML pattern or ng-pattern attributes present on the input element.
*/
@NgDirective(selector: '[ng-model][pattern]')
-@NgDirective(selector: '[ng-model][ng-pattern]', map: const {'ng-pattern': '=>pattern'})
-class NgModelPatternValidator extends _NgModelValidator {
+@NgDirective(
+ selector: '[ng-model][ng-pattern]',
+ map: const {'ng-pattern': '=>pattern'})
+class NgModelPatternValidator implements NgValidatable {
RegExp _pattern;
- get name => 'pattern';
+ String get name => 'pattern';
- NgModelPatternValidator(dom.Element inputElement, NgModel ngModel, Scope scope):
- super(inputElement, ngModel, scope) {
- listen();
- }
-
- bool isValid() {
- if(_pattern != null && value != null && value.length > 0) {
- return _pattern.hasMatch(ngModel.viewValue);
- }
+ NgModelPatternValidator(NgModel ngModel) {
+ ngModel.addValidator(this);
+ }
+ bool isValid(value) {
//remember, only required validates for the input being empty
- return true;
+ return _pattern == null || value == null || value.length == 0 ||
+ _pattern.hasMatch(value);
}
@NgAttr('pattern')
- get pattern => _pattern;
- set pattern(val) {
- if(val != null && val.length > 0) {
- _pattern = new RegExp(val);
- listen();
- } else {
- _pattern = null;
- unlisten();
- }
- }
+ set pattern(val) =>
+ _pattern = val != null && val.length > 0 ? new RegExp(val) : null;
}
/**
- * Validates the model to see if the length of its contents are greater than or equal to the minimum length
- * set in place by the HTML minlength or ng-minlength attributes present on the input element.
+ * Validates the model to see if the length of its contents are greater than or
+ * equal to the minimum length set in place by the HTML minlength or
+ * ng-minlength attributes present on the input element.
*/
@NgDirective(selector: '[ng-model][minlength]')
-@NgDirective(selector: '[ng-model][ng-minlength]', map: const {'ng-minlength': '=>minlength'})
-class NgModelMinLengthValidator extends _NgModelValidator {
+@NgDirective(
+ selector: '[ng-model][ng-minlength]',
+ map: const {'ng-minlength': '=>minlength'})
+class NgModelMinLengthValidator implements NgValidatable {
int _minlength;
- get name => 'minlength';
+ String get name => 'minlength';
- NgModelMinLengthValidator(dom.Element inputElement, NgModel ngModel, Scope scope):
- super(inputElement, ngModel, scope) {
- listen();
- }
+ NgModelMinLengthValidator(NgModel ngModel) {
+ ngModel.addValidator(this);
+ }
- bool isValid() {
+ bool isValid(value) {
//remember, only required validates for the input being empty
- if(_minlength == 0 || value == null || value.length == 0) {
- return true;
- }
- return value.length >= _minlength;
+ return _minlength == 0 || value == null || value.length == 0 ||
+ value.length >= _minlength;
}
@NgAttr('minlength')
- get minlength => _minlength;
- set minlength(value) {
- _minlength = value == null ? 0 : int.parse(value.toString());
- }
+ set minlength(value) =>
+ _minlength = value == null ? 0 : int.parse(value.toString());
}
/**
- * Validates the model to see if the length of its contents are less than or equal to the maximum length
- * set in place by the HTML maxlength or ng-maxlength attributes present on the input element.
+ * Validates the model to see if the length of its contents are less than or
+ * equal to the maximum length set in place by the HTML maxlength or
+ * ng-maxlength attributes present on the input element.
*/
@NgDirective(selector: '[ng-model][maxlength]')
-@NgDirective(selector: '[ng-model][ng-maxlength]', map: const {'ng-maxlength': '=>maxlength'})
-class NgModelMaxLengthValidator extends _NgModelValidator {
+@NgDirective(
+ selector: '[ng-model][ng-maxlength]',
+ map: const {'ng-maxlength': '=>maxlength'})
+class NgModelMaxLengthValidator implements NgValidatable {
int _maxlength = 0;
- get name => 'maxlength';
+ String get name => 'maxlength';
- NgModelMaxLengthValidator(dom.Element inputElement, NgModel ngModel, Scope scope):
- super(inputElement, ngModel, scope) {
- listen();
- }
-
- bool isValid() {
- return _maxlength == 0 || (value == null ? 0 : value.length) <= _maxlength;
+ NgModelMaxLengthValidator(NgModel ngModel) {
+ ngModel.addValidator(this);
}
+ bool isValid(value) =>
+ _maxlength == 0 || (value == null ? 0 : value.length) <= _maxlength;
+
@NgAttr('maxlength')
- get maxlength => _maxlength;
- set maxlength(value) {
- int length = value == null ? 0 : int.parse(value.toString());
- if(length != maxlength) {
- _maxlength = length;
- }
- }
+ set maxlength(value) =>
+ _maxlength = value == null ? 0 : int.parse(value.toString());
}
« no previous file with comments | « third_party/pkg/angular/lib/directive/ng_model.dart ('k') | third_party/pkg/angular/lib/directive/ng_non_bindable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698