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

Commit

Permalink
feat(forms): append valid/invalid CSS classes for each validator on a…
Browse files Browse the repository at this point in the history
…ll controls
  • Loading branch information
matsko authored and mhevery committed Mar 13, 2014
1 parent 419e918 commit 574065f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/directive/ng_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,23 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
* error is real).
*/
updateControlValidity(NgControl ngModel, String errorType, bool isValid) {
String validClassName = errorType + '-valid';
String invalidClassName = errorType + '-invalid';

if (isValid) {
if (errors.containsKey(errorType)) {
Set errorsByName = errors[errorType];
errorsByName.remove(ngModel);
if (errorsByName.isEmpty) {
errors.remove(errorType);
element..removeClass(invalidClassName)..addClass(validClassName);
}
}
if (errors.isEmpty) {
valid = true;
}
} else {
element..removeClass(validClassName)..addClass(invalidClassName);
errors.putIfAbsent(errorType, () => new Set()).add(ngModel);
invalid = true;
}
Expand Down
61 changes: 61 additions & 0 deletions test/directive/ng_form_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,53 @@ void main() {
}));
});

describe('validators', () {
it('should display the valid and invalid CSS classes on the element for each validation',
inject((TestBed _, Scope scope) {

var form = _.compile(
'<form name="myForm">' +
' <input type="text" ng-model="myModel" required />' +
'</form>'
);

scope.apply();

expect(form.classes.contains('ng-required-invalid')).toBe(true);
expect(form.classes.contains('ng-required-valid')).toBe(false);

scope.apply(() {
scope.context['myModel'] = 'value';
});

expect(form.classes.contains('ng-required-valid')).toBe(true);
expect(form.classes.contains('ng-required-invalid')).toBe(false);
}));

it('should display the valid and invalid CSS classes on the element for custom validations', () {
module((Module module) {
module.type(MyCustomFormValidator);
});
inject((TestBed _, Scope scope) {
var form = _.compile('<form name="myForm">' +
' <input type="text" ng-model="myModel" custom-form-validation />' +
'</form>');

scope.apply();

expect(form.classes.contains('custom-invalid')).toBe(true);
expect(form.classes.contains('custom-valid')).toBe(false);

scope.apply(() {
scope.context['myModel'] = 'yes';
});

expect(form.classes.contains('custom-valid')).toBe(true);
expect(form.classes.contains('custom-invalid')).toBe(false);
});
});
});

describe('reset()', () {
it('should reset the model value to its original state', inject((TestBed _) {
_.compile('<form name="superForm">' +
Expand Down Expand Up @@ -661,3 +708,17 @@ void main() {
});
});
}

@NgDirective(
selector: '[custom-form-validation]')
class MyCustomFormValidator extends NgValidator {
MyCustomFormValidator(NgModel ngModel) {
ngModel.addValidator(this);
}

final String name = 'custom';

bool isValid(name) {
return name != null && name == 'yes';
}
}
57 changes: 56 additions & 1 deletion test/directive/ng_model_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ void main() {
TestBed _;

beforeEach(module((Module module) {
module..type(ControllerWithNoLove);
module.type(ControllerWithNoLove);
module.type(MyCustomInputValidator);
}));

beforeEach(inject((TestBed tb) => _ = tb));
Expand Down Expand Up @@ -1307,6 +1308,46 @@ void main() {
expect(model.untouched).toBe(true);
});

describe('validators', () {
it('should display the valid and invalid CSS classes on the element for each validation',
inject((TestBed _, Scope scope) {

var input = _.compile('<input type="email" ng-model="myModel" />');

scope.apply(() {
scope.context['myModel'] = 'value';
});

expect(input.classes.contains('ng-email-invalid')).toBe(true);
expect(input.classes.contains('ng-email-valid')).toBe(false);

scope.apply(() {
scope.context['myModel'] = 'value@email.com';
});

expect(input.classes.contains('ng-email-valid')).toBe(true);
expect(input.classes.contains('ng-email-invalid')).toBe(false);
}));

it('should display the valid and invalid CSS classes on the element for custom validations',
inject((TestBed _, Scope scope) {

var input = _.compile('<input type="text" ng-model="myModel" custom-input-validation />');

scope.apply();

expect(input.classes.contains('custom-invalid')).toBe(true);
expect(input.classes.contains('custom-valid')).toBe(false);

scope.apply(() {
scope.context['myModel'] = 'yes';
});

expect(input.classes.contains('custom-valid')).toBe(true);
expect(input.classes.contains('custom-invalid')).toBe(false);
}));
});

describe('converters', () {
it('should parse the model value according to the given parser', inject((Scope scope) {
_.compile('<input type="text" ng-model="model" probe="i">');
Expand Down Expand Up @@ -1439,3 +1480,17 @@ class VowelValueParser implements NgModelConverter {
return value;
}
}

@NgDirective(
selector: '[custom-input-validation]')
class MyCustomInputValidator extends NgValidator {
MyCustomInputValidator(NgModel ngModel) {
ngModel.addValidator(this);
}

final String name = 'custom';

bool isValid(name) {
return name != null && name == 'yes';
}
}

0 comments on commit 574065f

Please sign in to comment.