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

Commit

Permalink
fix(forms): ensure fields, fieldsets & forms are marked as dirty when…
Browse files Browse the repository at this point in the history
… modified
  • Loading branch information
matsko committed Feb 6, 2014
1 parent 79b7525 commit ad60d55
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/directive/ng_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ class NgForm extends NgControl implements NgDetachAware, Map<String, NgModel> {
}
}

set dirty(value) {
super.dirty = _parentForm.dirty = true;
}

bool get isEmpty => false;
bool get isNotEmpty => !isEmpty;
get values => null;
Expand Down
5 changes: 5 additions & 0 deletions lib/directive/ng_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ class NgModel extends NgControl {
validate();
}

set dirty(value) {
super.dirty = _form.dirty = true;
}

/**
* Removes the model from the control/form.
*/
Expand Down Expand Up @@ -221,6 +225,7 @@ class InputTextLikeDirective {
processValue() {
var value = typedValue;
if (value != ngModel.viewValue) {
ngModel.dirty = true;
scope.$apply(() => ngModel.viewValue = value);
}
ngModel.validate();
Expand Down
38 changes: 38 additions & 0 deletions test/directive/ng_model_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,44 @@ describe('ng-model', () {
expect(element.classes.contains('ng-pristine')).toBe(true);
expect(element.classes.contains('ng-dirty')).toBe(false);
}));

it('should render the parent form/fieldset as dirty', inject((Scope scope) {
_.compile('<form name="myForm">' +
' <fieldset name="myFieldset">' +
' <input type="text" ng-model="my_model" probe="i" />' +
' </fieldset>' +
'</form>');

Probe inputProbe = _.rootScope.i;
var inputElement = inputProbe.element;

NgForm form = _.rootScope.myForm;
var formElement = form.element;

NgForm fieldset = _.rootScope.myFieldset;
var fieldsetElement = fieldset.element;

expect(formElement.classes.contains('ng-pristine')).toBe(true);
expect(formElement.classes.contains('ng-dirty')).toBe(false);

expect(fieldsetElement.classes.contains('ng-pristine')).toBe(true);
expect(fieldsetElement.classes.contains('ng-dirty')).toBe(false);

expect(inputElement.classes.contains('ng-pristine')).toBe(true);
expect(inputElement.classes.contains('ng-dirty')).toBe(false);

inputElement.value = '...hi...';
_.triggerEvent(inputElement, 'change');

expect(formElement.classes.contains('ng-pristine')).toBe(false);
expect(formElement.classes.contains('ng-dirty')).toBe(true);

expect(fieldsetElement.classes.contains('ng-pristine')).toBe(false);
expect(fieldsetElement.classes.contains('ng-dirty')).toBe(true);

expect(inputElement.classes.contains('ng-pristine')).toBe(false);
expect(inputElement.classes.contains('ng-dirty')).toBe(true);
}));
});

describe('valid / invalid', () {
Expand Down

0 comments on commit ad60d55

Please sign in to comment.