Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 7b75af4

Browse files
committed
feat(forms): introduce the control.hasError helper method
1 parent 476a8db commit 7b75af4

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

lib/directive/ng_control.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ abstract class NgControl implements NgDetachAware {
4949
untouched = true;
5050
}
5151

52+
bool hasError(String key) => errors.containsKey(key);
53+
5254
_onSubmit(bool valid) {
5355
if (valid) {
5456
_submit_valid = true;
@@ -241,5 +243,6 @@ class NgNullControl implements NgControl {
241243

242244
reset() => null;
243245
detach() => null;
246+
bool hasError(String key) => false;
244247

245248
}

test/directive/ng_form_spec.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,32 @@ void main() {
342342
}));
343343
});
344344

345+
describe('error handling', () {
346+
it('should return true or false depending on if an error exists on a form',
347+
inject((Scope scope, TestBed _) {
348+
349+
var element = $('<form name="myForm">'
350+
' <input type="text" ng-model="input" name="input" />' +
351+
'</form>');
352+
353+
_.compile(element);
354+
scope.apply();
355+
356+
var form = scope.context['myForm'];
357+
NgModel input = form['input'];
358+
359+
expect(form.hasError('big-failure')).toBe(false);
360+
361+
form.updateControlValidity(input, "big-failure", false);
362+
363+
expect(form.hasError('big-failure')).toBe(true);
364+
365+
form.updateControlValidity(input, "big-failure", true);
366+
367+
expect(form.hasError('big-failure')).toBe(false);
368+
}));
369+
});
370+
345371
describe('reset()', () {
346372
it('should reset the model value to its original state', inject((TestBed _) {
347373
_.compile('<form name="superForm">' +

test/directive/ng_model_spec.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,30 @@ void main() {
981981
}));
982982
});
983983

984+
describe('error handling', () {
985+
it('should return true or false depending on if an error exists on a form',
986+
inject((Scope scope, TestBed _) {
987+
988+
var element = $('<input type="text" ng-model="input" name="input" probe="i" />');
989+
990+
_.compile(element);
991+
scope.apply();
992+
993+
Probe p = scope.context['i'];
994+
NgModel model = p.directive(NgModel);
995+
996+
expect(model.hasError('big-failure')).toBe(false);
997+
998+
model.setValidity("big-failure", false);
999+
1000+
expect(model.hasError('big-failure')).toBe(true);
1001+
1002+
model.setValidity("big-failure", true);
1003+
1004+
expect(model.hasError('big-failure')).toBe(false);
1005+
}));
1006+
});
1007+
9841008
describe('text-like events', () {
9851009
it('should update the binding on the "input" event', inject(() {
9861010
_.compile('<input type="text" ng-model="model" probe="p">');

0 commit comments

Comments
 (0)