fix(forms): introduce checkbox required validator

Closes #11459
Closes #13364
This commit is contained in:
Dzmitry Shylovich
2016-12-10 13:44:04 +03:00
committed by Victor Berchet
parent 7b0a86718c
commit 2bf1bbc071
8 changed files with 172 additions and 16 deletions

View File

@ -39,7 +39,8 @@ export function main() {
ValidationBindingsForm,
UniqLoginValidator,
UniqLoginWrapper,
NestedFormGroupComp
NestedFormGroupComp,
FormControlCheckboxRequiredValidator,
]
});
});
@ -1320,6 +1321,24 @@ export function main() {
});
describe('validations', () => {
it('required validator should validate checkbox', () => {
const fixture = TestBed.createComponent(FormControlCheckboxRequiredValidator);
const control = new FormControl(false, Validators.requiredTrue);
fixture.componentInstance.control = control;
fixture.detectChanges();
const checkbox = fixture.debugElement.query(By.css('input'));
expect(checkbox.nativeElement.checked).toBe(false);
expect(control.hasError('required')).toEqual(true);
checkbox.nativeElement.checked = true;
dispatchEvent(checkbox.nativeElement, 'change');
fixture.detectChanges();
expect(checkbox.nativeElement.checked).toBe(true);
expect(control.hasError('required')).toEqual(false);
});
it('should use sync validators defined in html', () => {
const fixture = TestBed.createComponent(LoginIsEmptyWrapper);
const form = new FormGroup({
@ -2061,6 +2080,14 @@ class ValidationBindingsForm {
pattern: string;
}
@Component({
selector: 'form-control-checkbox-validator',
template: `<input type="checkbox" [formControl]="control">`
})
class FormControlCheckboxRequiredValidator {
control: FormControl;
}
@Component({
selector: 'uniq-login-wrapper',
template: `