Angular 4 remove required validator conditionally

后端 未结 10 1899
[愿得一人]
[愿得一人] 2021-02-01 01:26

In Angular 4 app I have a form model like this:

this.form = this._fb.group({
    title: [\'\', [Validators.required, Validators.minLength(3), Validators.maxLengt         


        
10条回答
  •  执笔经年
    2021-02-01 02:11

    this.fromName.get("formControlName").setValidators([Validators.required]);//setting validation
    this.fromName.get("formControlName").setErrors({'required':true});//error message
    this.fromName.updateValueAndValidity();//update validation
    
    
    this.fromName.get("formControlName").clearValidators([Validators.required]);//clear validation
    this.fromName.get("formControlName").setErrors(null);//updating error message
    this.fromName.updateValueAndValidity();//update validation
    

提交回复
热议问题