0

I am using Angular and the FormBuilder to generate the FormGroup. I want to be able to disable the status control if there is no value entered in the legacyId text box.

I was hoping the below would work, but the formGroup I am referencing on the status line is not yet created, its calling its own formGroup. How would I be able to do this so that the status gets disabled if the textbox is empty?

this.formGroup = this.formBuilder.group({
    legacyId: [{value: this.myItems.legacyId : ''), [Validators.maxLength(this.legacyIdMaxLength)]],
    status: [{value: this.statuses.find(x => x.id === status), disabled: this.formGroup.value.legacyId === ''}, [Validators.required]],
});

1 Answer 1

2

Maybe you can try with valueChanges like :

this.formGroup.get('legacyId').valueChanges.subscribe(value => {
  if (!!value) {
    this.formGroup.get('status').disable();
  } else {
    this.formGroup.get('status').enable();
  }
  this.formGroup.updateValueAndValidity()
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, yes that seems to work, had to wrap the enable in an else, but that seems to work
I've updated my code so people in the futur could find the solution quickly ;)

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.