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

fix(ngModel): ensure checkboxes and radio buttons are flagged as dirty when changed #585

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/directive/ng_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class InputCheckboxDirective {
};
inputElement.onChange.listen((value) {
scope.$apply(() {
ngModel.dirty = true;
ngModel.viewValue = inputElement.checked
? ngTrueValue.readValue(inputElement)
: ngFalseValue.readValue(inputElement);
Expand Down Expand Up @@ -367,6 +368,7 @@ class InputRadioDirective {
};
radioButtonElement.onClick.listen((_) {
if (radioButtonElement.checked) {
ngModel.dirty = true;
scope.$apply(() => ngModel.viewValue = ngValue.readValue(radioButtonElement));
}
});
Expand Down
52 changes: 52 additions & 0 deletions test/directive/ng_model_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,20 @@ describe('ng-model', () {
expect(element.checked).toBe(false);
}));

it('should render as dirty when checked', inject((Scope scope) {
var element = _.compile('<input type="text" ng-model="my_model" probe="i" />');
Probe probe = _.rootScope.i;
var model = probe.directive(NgModel);

expect(model.pristine).toEqual(true);
expect(model.dirty).toEqual(false);

_.triggerEvent(element, 'change');

expect(model.pristine).toEqual(false);
expect(model.dirty).toEqual(true);
}));


it('should update input value from model using ng-true-value/false', inject((Scope scope) {
var element = _.compile('<input type="checkbox" ng-model="model" ng-true-value="1" ng-false-value="0">');
Expand Down Expand Up @@ -621,6 +635,44 @@ describe('ng-model', () {
expect(greenBtn.checked).toBe(true);
expect(blueBtn.checked).toBe(false);
});

it('should render as dirty when checked', inject((Scope scope) {
var element = _.compile(
'<div>' +
' <input type="radio" id="on" ng-model="my_model" probe="i" value="on" />' +
' <input type="radio" id="off" ng-model="my_model" probe="j" value="off" />' +
'</div>'
);
Probe probe = _.rootScope.i;

var model = probe.directive(NgModel);

var input1 = element.query("#on");
var input2 = element.query("#off");

expect(model.pristine).toEqual(true);
expect(model.dirty).toEqual(false);

expect(input1.classes.contains("ng-dirty")).toBe(false);
expect(input2.classes.contains("ng-dirty")).toBe(false);
expect(input1.classes.contains("ng-pristine")).toBe(true);
expect(input1.classes.contains("ng-pristine")).toBe(true);

input1.checked = true;
_.triggerEvent(input1, 'click');

expect(model.pristine).toEqual(false);
expect(model.dirty).toEqual(true);

input1.checked = false;
input2.checked = true;
_.triggerEvent(input2, 'click');

expect(input1.classes.contains("ng-dirty")).toBe(true);
expect(input2.classes.contains("ng-dirty")).toBe(true);
expect(input1.classes.contains("ng-pristine")).toBe(false);
expect(input1.classes.contains("ng-pristine")).toBe(false);
}));
});

describe('type="search"', () {
Expand Down