Skip to content

Commit

Permalink
Use FormControl and only one of incompatible form options
Browse files Browse the repository at this point in the history
  • Loading branch information
willshowell committed Jun 26, 2017
1 parent ab870d1 commit 83954c5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/demo-app/input/input-demo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Component} from '@angular/core';
import {FormControl, Validators, NgControl} from '@angular/forms';
import {FormControl, Validators} from '@angular/forms';


let max = 5;
Expand Down Expand Up @@ -45,7 +45,7 @@ export class InputDemo {
}
}

customErrorStateMatcher(c: NgControl): boolean {
customErrorStateMatcher(c: FormControl): boolean {
const hasInteraction = c.dirty || c.touched;
const isInvalid = c.invalid;

Expand Down
27 changes: 11 additions & 16 deletions src/lib/core/error/error-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,31 @@
*/

import {InjectionToken} from '@angular/core';
import {NgControl, FormGroupDirective, NgForm} from '@angular/forms';
import {FormControl, FormGroupDirective, Form, NgForm} from '@angular/forms';

/** Injection token that can be used to specify the global error options. */
export const MD_ERROR_GLOBAL_OPTIONS = new InjectionToken<ErrorOptions>('md-error-global-options');

export type ErrorStateMatcher =
(control: NgControl, parentFormGroup: FormGroupDirective, parentForm: NgForm) => boolean;
(control: FormControl, form: FormGroupDirective | NgForm) => boolean;

export interface ErrorOptions {
errorStateMatcher?: ErrorStateMatcher;
}

export function defaultErrorStateMatcher(control: NgControl, formGroup: FormGroupDirective,
form: NgForm): boolean {

const isInvalid = control && control.invalid;
const isTouched = control && control.touched;
const isSubmitted = (formGroup && formGroup.submitted) ||
(form && form.submitted);
export function defaultErrorStateMatcher(control: FormControl, form: FormGroupDirective | NgForm) {
const isInvalid = control.invalid;
const isTouched = control.touched;
const isSubmitted = form && form.submitted;

return !!(isInvalid && (isTouched || isSubmitted));
}

export function showOnDirtyErrorStateMatcher(control: NgControl, formGroup: FormGroupDirective,
form: NgForm): boolean {

const isInvalid = control && control.invalid;
const isDirty = control && control.dirty;
const isSubmitted = (formGroup && formGroup.submitted) ||
(form && form.submitted);
export function showOnDirtyErrorStateMatcher(control: FormControl,
form: FormGroupDirective | NgForm) {
const isInvalid = control.invalid;
const isDirty = control.dirty;
const isSubmitted = form && form.submitted;

return !!(isInvalid && (isDirty || isSubmitted));
}
16 changes: 10 additions & 6 deletions src/lib/input/input-container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,10 +760,12 @@ describe('MdInputContainer', function () {
let component = fixture.componentInstance;
let containerEl = fixture.debugElement.query(By.css('md-input-container')).nativeElement;

expect(component.formControl.invalid).toBe(true, 'Expected form control to be invalid');
const control = component.formGroup.get('name')!;

expect(control.invalid).toBe(true, 'Expected form control to be invalid');
expect(containerEl.querySelectorAll('md-error').length).toBe(0, 'Expected no error messages');

component.formControl.markAsTouched();
control.markAsTouched();
fixture.detectChanges();

fixture.whenStable().then(() => {
Expand Down Expand Up @@ -1136,10 +1138,10 @@ class MdInputContainerWithFormErrorMessages {

@Component({
template: `
<form #form="ngForm" novalidate>
<form [formGroup]="formGroup">
<md-input-container>
<input mdInput
[formControl]="formControl"
formControlName="name"
[errorStateMatcher]="customErrorStateMatcher.bind(this)">
<md-hint>Please type something</md-hint>
<md-error>This field is required</md-error>
Expand All @@ -1148,8 +1150,10 @@ class MdInputContainerWithFormErrorMessages {
`
})
class MdInputContainerWithCustomErrorStateMatcher {
@ViewChild('form') form: NgForm;
formControl = new FormControl('', Validators.required);
formGroup = new FormGroup({
name: new FormControl('', Validators.required)
});

errorState = false;

customErrorStateMatcher(): boolean {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/input/input-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '@angular/core';
import {animate, state, style, transition, trigger} from '@angular/animations';
import {coerceBooleanProperty, Platform} from '../core';
import {FormGroupDirective, NgControl, NgForm} from '@angular/forms';
import {FormGroupDirective, NgControl, NgForm, FormControl} from '@angular/forms';
import {getSupportedInputTypes} from '../core/platform/features';
import {
getMdInputContainerDuplicatedHintError,
Expand Down Expand Up @@ -264,7 +264,8 @@ export class MdInputDirective {
/** Whether the input is in an error state. */
_isErrorState(): boolean {
const control = this._ngControl;
return this.errorStateMatcher(control, this._parentFormGroup, this._parentForm);
const form = this._parentFormGroup || this._parentForm;
return control && this.errorStateMatcher(control.control as FormControl, form);
}

/** Make sure the input is a supported type. */
Expand Down
10 changes: 8 additions & 2 deletions src/lib/input/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,14 @@ the error messages.
```

```ts
function myErrorStateMatcher(control: NgControl, parentFg: FormGroupDirective, parentForm: NgForm): boolean {
return !!(control.invalid && control.dirty);
function myErrorStateMatcher(control: FormControl, form: FormGroupDirective | NgForm): boolean {
// Error when invalid control is dirty, touched, or submitted
const isInvalid = control.invalid;
const isDirty = control.dirty;
const isTouched = control.touched;
const isSubmitted = form && form.submitted;

return !!(isInvalid && (isDirty || isTouched || isSubmitted)));
}
```

Expand Down

0 comments on commit 83954c5

Please sign in to comment.