Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(checkbox): Add RequiredTrue validator for md-checkbox #6006

Merged
merged 7 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/lib/checkbox/checkbox-required-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {
Directive,
forwardRef,
Provider,
} from '@angular/core';
import {
CheckboxRequiredValidator,
NG_VALIDATORS,
} from '@angular/forms';

export const _MdCheckboxRequiredValidator = CheckboxRequiredValidator;

export const MD_CHECKBOX_REQUIRED_VALIDATOR: Provider = {
provide: NG_VALIDATORS,
useExisting: forwardRef(() => MdCheckboxRequiredValidator),
multi: true
};

/**
* Validator for Material checkbox's required attribute in template-driven checkbox.
* Current CheckboxRequiredValidator only work with `input type=checkbox` and does not
* work with `md-checkbox`.
*/
@Directive({
selector:
'md-checkbox[required][formControlName],' +
'mat-checkbox[required][formControlName],' +
'md-checkbox[required][formControl],md-checkbox[required][ngModel],' +
'mat-checkbox[required][formControl],mat-checkbox[required][ngModel]',
providers: [MD_CHECKBOX_REQUIRED_VALIDATOR],
host: {'[attr.required]': 'required ? "" : null'}
})
export class MdCheckboxRequiredValidator extends _MdCheckboxRequiredValidator {}
47 changes: 46 additions & 1 deletion src/lib/checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('MdCheckbox', () => {
SingleCheckbox,
CheckboxWithFormDirectives,
MultipleCheckboxes,
CheckboxWithNgModel,
CheckboxWithTabIndex,
CheckboxWithAriaLabel,
CheckboxWithAriaLabelledby,
Expand Down Expand Up @@ -735,6 +736,41 @@ describe('MdCheckbox', () => {
});
});

describe('with required ngModel', () => {
let checkboxInstance: MdCheckbox;
let inputElement: HTMLInputElement;
let testComponent: CheckboxWithNgModel;

beforeEach(() => {
fixture = TestBed.createComponent(CheckboxWithNgModel);
fixture.detectChanges();

let checkboxDebugElement = fixture.debugElement.query(By.directive(MdCheckbox));
let checkboxNativeElement = checkboxDebugElement.nativeElement;
testComponent = fixture.debugElement.componentInstance;
checkboxInstance = checkboxDebugElement.componentInstance;
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
});

it('should validate with RequiredTrue validator', () => {
let checkboxElement = fixture.debugElement.query(By.directive(MdCheckbox));
let ngModel = checkboxElement.injector.get<NgModel>(NgModel);

testComponent.isRequired = true;
inputElement.click();
fixture.detectChanges();

expect(checkboxInstance.checked).toBe(true);
expect(ngModel.valid).toBe(true);

inputElement.click();
fixture.detectChanges();

expect(checkboxInstance.checked).toBe(false);
expect(ngModel.valid).toBe(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to check if the error is specifically due to the required validator?

});
});

describe('with name attribute', () => {
beforeEach(() => {
fixture = TestBed.createComponent(CheckboxWithNameAttribute);
Expand Down Expand Up @@ -874,7 +910,7 @@ class SingleCheckbox {
onCheckboxChange: (event?: MdCheckboxChange) => void = () => {};
}

/** Simple component for testing an MdCheckbox with ngModel. */
/** Simple component for testing an MdCheckbox with ngModel in a form. */
@Component({
template: `
<form>
Expand All @@ -886,6 +922,15 @@ class CheckboxWithFormDirectives {
isGood: boolean = false;
}

/** Simple component for testing an MdCheckbox with required ngModel. */
@Component({
template: `<md-checkbox [required]="isRequired" [(ngModel)]="isGood">Be good</md-checkbox>`,
})
class CheckboxWithNgModel {
isGood: boolean = false;
isRequired: boolean = true;
}

/** Simple test component with multiple checkboxes. */
@Component(({
template: `
Expand Down
7 changes: 4 additions & 3 deletions src/lib/checkbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import {CommonModule} from '@angular/common';
import {ObserveContentModule} from '@angular/cdk';
import {MdRippleModule, MdCommonModule, FocusOriginMonitor} from '../core';
import {MdCheckbox} from './checkbox';

import {MdCheckboxRequiredValidator} from './checkbox-required-validator';

@NgModule({
imports: [CommonModule, MdRippleModule, MdCommonModule, ObserveContentModule],
exports: [MdCheckbox, MdCommonModule],
declarations: [MdCheckbox],
exports: [MdCheckbox, MdCheckboxRequiredValidator, MdCommonModule],
declarations: [MdCheckbox, MdCheckboxRequiredValidator],
providers: [FocusOriginMonitor]
})
export class MdCheckboxModule {}


export * from './checkbox';
export * from './checkbox-required-validator';