Created
April 13, 2016 04:56
-
-
Save lricoy/8c358b8f635d0731eef0022779b82bcc to your computer and use it in GitHub Desktop.
Validation messages directive
This file contains hidden or 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
import {Component, Host} from 'angular2/core'; | |
import {NgFormModel} from 'angular2/common'; | |
import {ValidationService} from './validation.service'; | |
@Component({ | |
selector: 'control-messages', | |
inputs: ['controlName: control'], | |
template: `<p *ngIf="errorMessage !== null">{{errorMessage}}</p>` | |
}) | |
export class ControlMessages { | |
controlName: string; | |
constructor(@Host() private _formDir: NgFormModel) { } | |
get errorMessage():string { | |
// Acha o controle no component pai (Host/Parent) | |
let c = this._formDir.form.find(this.controlName); | |
// Para cada attr no objeto errors | |
for (let propertyName in c.errors) { | |
// Confirma que existe um erro e que o componente foi "tocado" | |
if (c.errors.hasOwnProperty(propertyName) && c.touched) { | |
return ValidationService.getValidatorErrorMessage(propertyName); | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment