diff --git a/lib/directive/ng_form.dart b/lib/directive/ng_form.dart index 69dbd22be..a9651e4f4 100644 --- a/lib/directive/ng_form.dart +++ b/lib/directive/ng_form.dart @@ -148,6 +148,10 @@ class NgForm extends NgControl implements NgDetachAware, Map { } } + set dirty(value) { + super.dirty = _parentForm.dirty = true; + } + bool get isEmpty => false; bool get isNotEmpty => !isEmpty; get values => null; diff --git a/lib/directive/ng_model.dart b/lib/directive/ng_model.dart index c10ec9c1e..2eb46f672 100644 --- a/lib/directive/ng_model.dart +++ b/lib/directive/ng_model.dart @@ -132,6 +132,10 @@ class NgModel extends NgControl { validate(); } + set dirty(value) { + super.dirty = _form.dirty = true; + } + /** * Removes the model from the control/form. */ @@ -221,6 +225,7 @@ class InputTextLikeDirective { processValue() { var value = typedValue; if (value != ngModel.viewValue) { + ngModel.dirty = true; scope.$apply(() => ngModel.viewValue = value); } ngModel.validate(); diff --git a/test/directive/ng_model_spec.dart b/test/directive/ng_model_spec.dart index b0c81bab6..ff1bd27e9 100644 --- a/test/directive/ng_model_spec.dart +++ b/test/directive/ng_model_spec.dart @@ -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('
' + + '
' + + ' ' + + '
' + + '
'); + + 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', () {