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

Commit

Permalink
fix(forms): do not reset input fields on valid submission
Browse files Browse the repository at this point in the history
  • Loading branch information
matsko authored and jbdeboer committed Mar 5, 2014
1 parent 23be9b2 commit 24e9c3d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/directive/ng_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ class NgForm extends NgControl implements Map<String, NgControl> {
if (!element.attributes.containsKey('action')) {
element.onSubmit.listen((event) {
event.preventDefault();
_scope.broadcast('submitNgControl', valid == null ? false : valid);
reset();
_scope.broadcast('submitNgControl', valid == true);
if (valid == true) {
reset();
}
});
}
}
Expand Down
7 changes: 7 additions & 0 deletions lib/directive/ng_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class NgModel extends NgControl implements NgAttachAware {
modelValue = _lastValue;
}

_onSubmit(bool valid) {
super._onSubmit(valid);
if (valid) {
_lastValue = modelValue;
}
}

@NgAttr('name')
get name => _name;
set name(value) {
Expand Down
31 changes: 27 additions & 4 deletions test/directive/ng_form_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ void main() {
expect(model.viewValue).toEqual('animal');
}));

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

expect(formModel.touched).toBe(true);
}));

it('should reset each of the controls to be untouched only when the form has a valid submission', inject((Scope scope, TestBed _) {
var form = _.compile('<form name="duperForm">' +
' <input type="text" ng-model="myModel" probe="i" required />' +
'</form>');

NgForm formModel = _.rootScope.context['duperForm'];
var model = _.rootScope.context['i'].directive(NgModel);
var input = model.element;
_.triggerEvent(input, 'blur');

expect(formModel.touched).toBe(true);
expect(model.touched).toBe(true);
expect(formModel.invalid).toBe(true);

_.triggerEvent(form, 'submit');

expect(formModel.touched).toBe(true);
expect(model.touched).toBe(true);
expect(formModel.invalid).toBe(true);

scope.apply(() {
scope.context['myModel'] = 'value';
});
_.triggerEvent(form, 'submit');

expect(formModel.valid).toBe(true);
expect(formModel.touched).toBe(false);
expect(formModel.untouched).toBe(true);
expect(form.classes.contains('ng-touched')).toBe(false);
expect(form.classes.contains('ng-untouched')).toBe(true);
expect(model.touched).toBe(false);
}));
});

Expand Down

0 comments on commit 24e9c3d

Please sign in to comment.