projects/i-components/src/lib/directives/validationsupport.directive.ts
Support directive for adding validation elements to a component template
Selector | [wmValidationSupport] |
Properties |
|
Methods |
Inputs |
constructor(elementRef: ElementRef, injectorBase: Injector)
|
|||||||||
Creates an instance of ValidationSupportDirective.
Parameters :
|
wmValidationSupport | |
Type : [boolean, string, string, string, ]
|
|
Pair of values representing the validation state (valid or not) and the validation error message |
ngOnChanges | ||||||
ngOnChanges(changes: SimpleChanges)
|
||||||
Verifies for changes in the validation state
Parameters :
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Clean up for the directive
Returns :
void
|
Private validationHelper |
Type : ValidationHelper
|
Helper used to register/unregister the validation message on the component. |
Public wmValidationSupport |
Type : [boolean, string, string, string, ]
|
Decorators :
@Input()
|
Pair of values representing the validation state (valid or not) and the validation error message |
import {
Directive,
ElementRef,
Injector,
Input,
OnChanges,
OnDestroy,
SimpleChanges,
} from '@angular/core';
import { ValidationHelper } from '../utils/validation-helper';
/**
* Support directive for adding validation elements to a component template
*
* @export
* @class ValidationSupportDirective
* @implements {OnChanges}
* @implements {OnDestroy}
*/
@Directive({
selector: '[wmValidationSupport]',
})
export class ValidationSupportDirective implements OnChanges, OnDestroy {
/**
* Pair of values representing the validation state (valid or not) and the validation error message
*
* @type {[boolean, string]}
* @memberof ValidationSupportDirective
*/
@Input() public wmValidationSupport: [
boolean,
string,
string,
string,
boolean?
];
/**
* Helper used to register/unregister the validation message
* on the component.
*
* @private
* @type {ValidationHelper}
* @memberof ValidationSupportDirective
*/
private validationHelper: ValidationHelper;
/**
* Creates an instance of ValidationSupportDirective.
*
* @param {ElementRef} elementRef
* @param {Injector} injectorBase
* @memberof ValidationSupportDirective
*/
constructor(elementRef: ElementRef, injectorBase: Injector) {
this.validationHelper = new ValidationHelper(elementRef, injectorBase);
}
/**
* Verifies for changes in the validation state
*
* @param {SimpleChanges} changes
* @memberof ValidationSupportDirective
*/
ngOnChanges(changes: SimpleChanges): void {
/* istanbul ignore else */
if (changes.wmValidationSupport) {
const [flag, message, applyTo, size, isRounded] =
this.wmValidationSupport;
if (flag) {
this.validationHelper.isRounded = isRounded;
this.validationHelper.registerValidationMsgService(
message,
size,
applyTo
);
} else {
this.validationHelper.unregisterValidationMsgService();
}
}
}
/**
* Clean up for the directive
*
* @memberof ValidationSupportDirective
*/
ngOnDestroy() {
this.validationHelper.unregisterValidationMsgService();
}
}