Skip to content

Instantly share code, notes, and snippets.

@dancornilov
Last active March 26, 2019 08:39
Show Gist options
  • Save dancornilov/0411de8657282b19406d63d1a6244c74 to your computer and use it in GitHub Desktop.
Save dancornilov/0411de8657282b19406d63d1a6244c74 to your computer and use it in GitHub Desktop.
Validator with Inputs
import { Component, Input, OnInit } from '@angular/core';
import { FormControl, FormGroup, ValidationErrors } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app-validator',
templateUrl: './validator.component.html',
styleUrls: ['./validator.component.scss']
})
export class ValidatorComponent implements OnInit {
@Input() public control: FormControl;
@Input() public customName: string;
@Input() public customValidation: { name: string, text: string };
public name: string;
constructor(private translateService: TranslateService) {
}
public ngOnInit(): void {
const fieldName = this.customName ? this.customName : this.getControlName();
this.name = this.translateService.instant(`fields.${fieldName}`);
}
/**
* Check if the validator has a custom validation message
*
* @param errors
*
* @return string
*/
public message(errors: ValidationErrors | null): string {
const properties = Object.keys(errors);
let customMessage: boolean;
if (this.customValidation) {
customMessage = (properties.indexOf(this.customValidation.name) > -1);
}
return customMessage ? this.customValidation.text : `validators.${properties[0]}`;
}
/**
* Extract control name from formGroup
*
* @return string
*/
public getControlName(): string {
let controlName = null;
const parent = this.control['parent'];
if (parent instanceof FormGroup) {
for (const name in parent.controls) {
if (this.control === parent.controls[name]) {
controlName = name;
}
}
}
return controlName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment