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

Commit 2928ae7

Browse files
matskomhevery
authored andcommitted
fix(forms): store models instead of controls within the collection of errors
1 parent bbbba4b commit 2928ae7

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

lib/directive/ng_control.dart

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
2020
final NgAnimate _animate;
2121
dom.Element _element;
2222

23-
final Map<String, List<NgControl>> errors = new Map<String, List<NgControl>>();
24-
final List<NgControl> _controls = new List<NgControl>();
25-
final Map<String, List<NgControl>> _controlByName = new Map<String, List<NgControl>>();
23+
final errors = new Map<String, Set<NgModel>>();
24+
final _controls = new List<NgControl>();
25+
final _controlByName = new Map<String, List<NgControl>>();
2626

2727
NgControl(dom.Element this._element, Injector injector,
2828
NgAnimate this._animate)
@@ -172,30 +172,23 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
172172
* * [isValid] - Whether the given error is valid or not (false would mean the
173173
* error is real).
174174
*/
175-
updateControlValidity(NgControl control, String errorType, bool isValid) {
176-
List queue = errors[errorType];
177-
175+
updateControlValidity(NgControl ngModel, String errorType, bool isValid) {
178176
if (isValid) {
179-
if (queue != null) {
180-
queue.remove(control);
181-
if (queue.isEmpty) {
177+
if (errors.containsKey(errorType)) {
178+
Set errorsByName = errors[errorType];
179+
errorsByName.remove(ngModel);
180+
if (errorsByName.isEmpty) {
182181
errors.remove(errorType);
183-
_parentControl.updateControlValidity(this, errorType, true);
184182
}
185183
}
186184
if (errors.isEmpty) {
187185
valid = true;
188186
}
189187
} else {
190-
if (queue == null) {
191-
queue = new List<NgControl>();
192-
errors[errorType] = queue;
193-
_parentControl.updateControlValidity(this, errorType, false);
194-
} else if (queue.contains(control)) return;
195-
196-
queue.add(control);
188+
errors.putIfAbsent(errorType, () => new Set()).add(ngModel);
197189
invalid = true;
198190
}
191+
_parentControl.updateControlValidity(ngModel, errorType, valid);
199192
}
200193
}
201194

test/directive/ng_form_spec.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,31 @@ void main() {
152152
expect(form.invalid).toBe(false);
153153
}));
154154

155+
it('should collect the invalid models upon failed validation', inject((Scope scope, TestBed _) {
156+
var element = $('<form name="myForm">'
157+
' <input type="text" ng-model="one" name="one" />' +
158+
' <input type="text" ng-model="two" name="two" />' +
159+
' <input type="text" ng-model="three" name="three" />' +
160+
'</form>');
161+
162+
_.compile(element);
163+
scope.apply();
164+
165+
var form = scope.context['myForm'];
166+
NgModel one = form['one'];
167+
NgModel two = form['two'];
168+
NgModel three = form['three'];
169+
170+
one.setValidity("email", false);
171+
two.setValidity("number", true);
172+
three.setValidity("format", false);
173+
174+
expect(form.errors.keys.length).toBe(2);
175+
expect(form.errors['email'].elementAt(0)).toBe(one);
176+
expect(form.errors['format'].elementAt(0)).toBe(three);
177+
}));
178+
179+
155180
it('should not handle the control errorType pair more than once', inject((Scope scope, TestBed _) {
156181
var element = $('<form name="myForm">'
157182
' <input type="text" ng-model="one" name="one" />' +

0 commit comments

Comments
 (0)