Display custom validator error with mat-error

后端 未结 3 1705
清酒与你
清酒与你 2021-01-30 11:16

I come to you for talking about a problem with angular material. In fact, I think it\'s an issue, but I prefer looking for a misunterstanding first.

The first thing abou

3条回答
  •  天涯浪人
    2021-01-30 11:35

    How to create a custom validation:

    if the internal property 'isValid' of the component is false, then set the input status in error, and display a message.

    HTML:

    
    
    
    Input not valid.
    
    

    TS:

    isValid = true;
    
    changeValitationStatus() {
    this.matcher = new InputErrorStateMatcher(!this.isValid);
    }
    
    matcher = new InputErrorStateMatcher(!this.isValid);
    
    
    
    class InputErrorStateMatcher implements ErrorStateMatcher {
        constructor(private errorstate: boolean) {}
        isErrorState(control: FormControl|null, form: FormGroupDirective|NgForm|null):boolean {
        return this.errorstate;
      }
    }
    

    And in this way you have a validation using only the formControl.

提交回复
热议问题