UNPKG

bia-framework

Version:

Bia Framework to work with angular 2

57 lines (47 loc) 1.9 kB
import {Directive, Host, Attribute, OnInit, Optional, Input} from "@angular/core"; import {CoreFormInput} from "../core-form-input/core-form-input.component"; import {AbstractControl} from "@angular/forms"; import {CoreFormDropDown} from "../core-form-dropdown/core-form-dropdown"; import {CoreFormControlBase} from "../CoreFormControlBase"; @Directive( { selector: 'validation-match' } ) export class CoreFormValidationMatch implements OnInit { private host:CoreFormControlBase; @Input("control-match") match:CoreFormControlBase; static matchValue:Object; ngOnInit():any { if (this.host != null) this.host.addFormValidator( {ValidationName: 'validation-match', ValidationMessage: this.validationMatchMessage}, CoreFormValidationMatch.matchValidation); this.match.bindChange.subscribe(( a )=> { CoreFormValidationMatch.matchValue = a; this.host.control.updateValueAndValidity(); }); return undefined; } constructor( @Optional() @Host() private inputHost:CoreFormInput, @Optional() @Host() private dropDownHost:CoreFormDropDown, @Attribute("message") private validationMatchMessage:string ) { this.host = inputHost || dropDownHost; } public static matchValidation( c:AbstractControl ):{ [s:string]:boolean } { if (CoreFormValidationMatch.matchValue != null && c.value != null) { if (CoreFormValidationMatch.matchValue == c.value) return null; else return {"validation-match": true}; } else return null; } }