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

Commit 24e9c3d

Browse files
matskojbdeboer
authored andcommitted
fix(forms): do not reset input fields on valid submission
1 parent 23be9b2 commit 24e9c3d

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

lib/directive/ng_form.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ class NgForm extends NgControl implements Map<String, NgControl> {
3939
if (!element.attributes.containsKey('action')) {
4040
element.onSubmit.listen((event) {
4141
event.preventDefault();
42-
_scope.broadcast('submitNgControl', valid == null ? false : valid);
43-
reset();
42+
_scope.broadcast('submitNgControl', valid == true);
43+
if (valid == true) {
44+
reset();
45+
}
4446
});
4547
}
4648
}

lib/directive/ng_model.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ class NgModel extends NgControl implements NgAttachAware {
4949
modelValue = _lastValue;
5050
}
5151

52+
_onSubmit(bool valid) {
53+
super._onSubmit(valid);
54+
if (valid) {
55+
_lastValue = modelValue;
56+
}
57+
}
58+
5259
@NgAttr('name')
5360
get name => _name;
5461
set name(value) {

test/directive/ng_form_spec.dart

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ void main() {
398398
expect(model.viewValue).toEqual('animal');
399399
}));
400400

401-
it('should set the form control to be untouched when the model is reset or submitted', inject((TestBed _) {
401+
it('should set the form control to be untouched when the model is reset', inject((TestBed _) {
402402
var form = _.compile('<form name="duperForm">' +
403403
' <input type="text" ng-model="myModel" probe="i" />' +
404404
'</form>');
@@ -429,13 +429,36 @@ void main() {
429429
_.triggerEvent(input, 'blur');
430430

431431
expect(formModel.touched).toBe(true);
432+
}));
433+
434+
it('should reset each of the controls to be untouched only when the form has a valid submission', inject((Scope scope, TestBed _) {
435+
var form = _.compile('<form name="duperForm">' +
436+
' <input type="text" ng-model="myModel" probe="i" required />' +
437+
'</form>');
438+
439+
NgForm formModel = _.rootScope.context['duperForm'];
440+
var model = _.rootScope.context['i'].directive(NgModel);
441+
var input = model.element;
442+
_.triggerEvent(input, 'blur');
443+
444+
expect(formModel.touched).toBe(true);
445+
expect(model.touched).toBe(true);
446+
expect(formModel.invalid).toBe(true);
432447

433448
_.triggerEvent(form, 'submit');
434449

450+
expect(formModel.touched).toBe(true);
451+
expect(model.touched).toBe(true);
452+
expect(formModel.invalid).toBe(true);
453+
454+
scope.apply(() {
455+
scope.context['myModel'] = 'value';
456+
});
457+
_.triggerEvent(form, 'submit');
458+
459+
expect(formModel.valid).toBe(true);
435460
expect(formModel.touched).toBe(false);
436-
expect(formModel.untouched).toBe(true);
437-
expect(form.classes.contains('ng-touched')).toBe(false);
438-
expect(form.classes.contains('ng-untouched')).toBe(true);
461+
expect(model.touched).toBe(false);
439462
}));
440463
});
441464

0 commit comments

Comments
 (0)