1

I am trying to find simple way to update the form fields with Validators. For now I do the below:

 ngOnInit() { 
        this.form.get('licenseType').valueChanges.subscribe(value => {
        this.licenseChange(value);
    })
 }

 licenseChange(licenseValue: any) {
    if (licenseValue === 2) {
        this.form.get('price').setValidators([Validators.required]);
        this.form.get('price').updateValueAndValidity();
        this.form.get('noOfLicenses').setValidators([Validators.required]);
        this.form.get('noOfLicenses').updateValueAndValidity();
        this.form.get('licenseKey').setValidators([Validators.required]);
        this.form.get('licenseKey').updateValueAndValidity();
        this.form.get('supportNo').setValidators([Validators.required]);
        this.form.get('supportNo').updateValueAndValidity();
        this.form.get('purchasedFrom').setValidators([Validators.required]);
        this.form.get('purchasedFrom').updateValueAndValidity();
        //......others follows here
    }
    else {
        this.form.get('price').clearValidators(); this.form.get('price').updateValueAndValidity();
        this.form.get('noOfLicenses').clearValidators(); this.form.get('noOfLicenses').updateValueAndValidity();
        this.form.get('licenseKey').clearValidators(); this.form.get('licenseKey').updateValueAndValidity();
        this.form.get('supportNo').clearValidators(); this.form.get('supportNo').updateValueAndValidity();
        this.form.get('purchasedFrom').clearValidators(); this.form.get('purchasedFrom').updateValueAndValidity();
        //......others follows here
    }       
}

Is this the only way to add and update validators or is there any other way to achieve this. For now I am calling the updateValueAndValidity() after setting/clearing each field.

Update

Something like

licenseChange(licenseValue: any) {
 if (licenseValue === 2) {
    this.form.get('price').setValidators([Validators.required]);        
    //......others follows here
 }
 else{
    //......
 }
}
this.form.updateValueAndValidity();///only one line at the bottom setting the entire fields.

2 Answers 2

2

I done something similar like this

 licenseChange(licenseValue: any) {
        if (licenseValue === 2) {
            this.updateValidation(true,this.form.get('price'));
            //......others follows here
        }
        else {
               this.updateValidation(false,this.form.get('price'));
            //......others follows here
        }       
    }  


    //TODO:To update formgroup validation
      updateValidation(value, control: AbstractControl) {
        if (value) {
            control.setValidators([Validators.required]);
        }else{
           control.clearValidators();
        } 
        control.updateValueAndValidity();
      }

If you want to do this for all the controlls inside your form

 licenseChange(licenseValue: any) {

    for (const field in this.form.controls) { // 'field' is a string

        const control = this.form.get(field); // 'control' is a FormControl

         (licenseValue === 2) ? this.updateValidation(true, 
             control):this.updateValidation(fasle, control);

    } 
}  
Sign up to request clarification or add additional context in comments.

Comments

0

I did it like below:

this.form.get('licenseType').valueChanges.subscribe(value => {
     this.licenseChange(value, this.form.get('price'));
     //....Others
}

licenseChange(licenseValue: any, control: AbstractControl) {
    licenseValue === 2 ? control.setValidators([Validators.required]) : control.clearValidators();
    control.updateValueAndValidity();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.