3

I want to disable the "addressLine1" validator; how can i disable it? seeing at it is nested inside of "address" element

 this.form = fb.group({           
            'region': [null, Validators.required],

            'address': fb.group({               
                'addressLine1': ["", Validators.required],
                'addressLine2': [""],
                'city': ["", Validators.required]             
            }),

        })

I tried this below but it did not work

 this.form.controls["address"]["addressLine1"] .disable();

Thank you

Here is the html:

<form [formGroup]="form">

<div ><label><span class="required">*</span>State/Province/Region<br><input class="form-control" pInputText formControlName="addressLine1" [(ngModel)]="selected.address.addressLine1" required></label></div>

</form>

2 Answers 2

5

The correct syntax should be:

(this.form.controls["address"] as FormGroup).controls["addressLine1"].disable({});

or even better

this.form.get('address.addressLine1').disable();

Stackblitz example

Update

To disable validator use the following code:

const control = this.form.get('address.addressLine1');
control.setValidators(null);
control.updateValueAndValidity();

Stackblitz Example

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

3 Comments

they disabled the whole field instead of just disabling the validator checks
What do you expect when calling disable method?
i want it to just disable the validator for that field so i can save the form without having input in that field
1

We need to pass parameters to disable method as docs says us:

enter image description here

So if you don't need to pass any parameters, just pass an empty object, and the code should look like this:

this.form.controls["address"]["addressLine1"].disable({});

8 Comments

@BadGuyKUTA, didn't you forget to add [formGroup]="form" to your form element and formControlName="addressLine1" (the example for addressLine1 control) on your inputs?
i just edited original post and added the html it has formControlName as that already
@BadGuyKUTA, and form tag looks like this: <form [formGroup]="form">?
Correct <form [formGroup]="form">; i think my error is how i am referencing it somewhere here this.form.controls["address"]["addressLine1"] .disable();
@BadGuyKUTA, I just tried in my project, it should be this.form.controls["address"]["addressLine1"].disable({});, we should pass a parameter to disable method. Try that, it should do the trick
|

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.