Skip to content

Instantly share code, notes, and snippets.

@lricoy
Created April 13, 2016 04:56
Show Gist options
  • Save lricoy/8c358b8f635d0731eef0022779b82bcc to your computer and use it in GitHub Desktop.
Save lricoy/8c358b8f635d0731eef0022779b82bcc to your computer and use it in GitHub Desktop.
Validation messages directive
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