Project

General

Profile

1
import { Component, Input, OnInit } from '@angular/core';
2
import { FormControl } from '@angular/forms';
3

    
4
@Component({
5
  selector: 'app-validation-message',
6
  templateUrl: './validation-message.component.html',
7
  styleUrls: ['./validation-message.component.scss']
8
})
9
export class ValidationMessageComponent implements OnInit {
10

    
11
  @Input() control: FormControl;
12
  @Input() validationMessage: String;
13

    
14
  @Input() set displayEvenIfPristine(val: boolean) {
15
    this._displayEvenIfPristine = val;
16
    this.shouldMessageBeShown();
17
  }
18

    
19
  _displayEvenIfPristine: boolean
20

    
21
  displayMessage: boolean;
22

    
23
  constructor() { }
24

    
25
  ngOnInit(): void {
26
    this.shouldMessageBeShown();
27
    this.control.statusChanges.subscribe(() => this.shouldMessageBeShown());
28
  }
29

    
30
  shouldMessageBeShown() {
31
    let temp = this.control.invalid && (this.control.dirty || this.control.touched || this._displayEvenIfPristine);
32

    
33
    this.displayMessage = temp;
34
  }
35
}
(4-4/4)