This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form): use md-message/md-messages for form validation errors
- support declaratively specifying validators in input basic usage example. - add md-messages directive for specifying a group to hold form messages. - add md-message to decorate elements inside of md-messages container to be associated with a particular error. - refactor messages and validation out of input/ path and into form/ path, which seems more appropriate.
- Loading branch information
1 parent
ebe8c36
commit 14e36f4
Showing
5 changed files
with
111 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import {CONST_EXPR} from "angular2/src/facade/lang"; | ||
import {NG_VALIDATORS} from "angular2/common"; | ||
import { | ||
Attribute, Input, Provider, Directive, Optional, SkipSelf, Host, OnDestroy, OnInit, | ||
ContentChildren, QueryList, Query, AfterContentInit | ||
} from "angular2/core"; | ||
import {Validator, NgFormModel, NgControlName} from "angular2/common"; | ||
import {Control} from "angular2/common"; | ||
import {isPresent} from "angular2/src/facade/lang"; | ||
|
||
|
||
@Directive({ | ||
selector: '[md-message]', | ||
host: { | ||
'[hidden]': 'okay' | ||
} | ||
}) | ||
export class MdMessage { | ||
@Input('md-message') errorKey: string; | ||
|
||
okay: boolean = true; | ||
|
||
constructor(@Attribute('md-message') pattern: any) { | ||
if (isPresent(pattern)) { | ||
this.errorKey = pattern; | ||
} | ||
} | ||
} | ||
|
||
|
||
@Directive({ | ||
selector: '[md-messages]', | ||
host: { | ||
'md-messages': '', | ||
'[hidden]': 'valid || !property?.touched', | ||
'[class.md-valid]': 'valid && property?.touched', | ||
'[class.md-invalid]': '!valid && property?.touched' | ||
} | ||
}) | ||
export class MdMessages implements OnInit, OnDestroy { | ||
|
||
@Input('md-messages') property: string|NgControlName; | ||
|
||
valid: boolean; | ||
|
||
constructor(@Query(MdMessage) public messages: QueryList<MdMessage>, | ||
@Optional() @SkipSelf() @Host() public form: NgFormModel, | ||
@Attribute('md-messages') pattern: any) { | ||
if (isPresent(pattern)) { | ||
this.property = pattern; | ||
} | ||
} | ||
|
||
private _unsubscribe: any = null; | ||
|
||
ngOnInit() { | ||
if (this.property instanceof NgControlName) { | ||
let ctrl: NgControlName = <NgControlName>this.property; | ||
this.form = ctrl.formDirective; | ||
this._unsubscribe = (<any>ctrl).update.subscribe(this._valueChanged.bind(this)); | ||
} | ||
else { | ||
if (!this.form) { | ||
throw new Error('md-messages cannot bind to text property without a parent NgFormModel'); | ||
} | ||
console.log('init formbuilder property: ' + this.property); | ||
} | ||
} | ||
|
||
ngOnDestroy(): any { | ||
this._unsubscribe(); | ||
} | ||
|
||
private _valueChanged(newValue: string) { | ||
let ctrl: NgControlName = <NgControlName>this.property; | ||
this.valid = !ctrl.errors; | ||
if (ctrl.errors) { | ||
this.messages.toArray().forEach((m: MdMessage) => { | ||
m.okay = !isPresent(ctrl.errors[m.errorKey]); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters