1

I have a reactive form in my app and I was creating it like this:

this.myForm = new FormGroup({
    name: new FormControl(this.item.name, Validators.required),
    active: new FormControl(this.item.active),
    value: new FormControl({ value: this.item.value, disabled: !this.item.active }, this.item.active ? Validators.required : null)
});

But now, I needed to add some custom validators to my form. So, I've changed my code to form builder like this:

this.myForm = this.fb.group({
    name: [this.item.name],
    active: [this.item.active],
    value: [this.item.value, this.item.active ? Validators.required : null]
}, {validators: customValidator()});

I couldn't find how to set disabled attribute like above to below.

1

1 Answer 1

1

Why you try to disable with disabled value. Use disable method of form instead.

this.myForm = new FormGroup({
    name: new FormControl(this.item.name, Validators.required),
    active: new FormControl(this.item.active),
    value: new FormControl({ value: this.item.value}, 
   this.item.active ? Validators.required : null)
},{validators: customValidator()} );

then

this.myForm.get('value').disable({emitEvent: false});

emitEvent: false here will prevent to trigger value change here.

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

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.