disabling mdb-select in a reactive form


Topic: disabling mdb-select in a reactive form

mdb2 pro asked 6 years ago

When disabling mdb-select in a reactive form using [disabled]="isEditable" causes the following warning: It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true when you set up this control in your component class, the disabled attribute will actually be set in the DOM for you. We recommend using this approach to avoid 'changed after checked' errors. Example: form = new FormGroup({ first: new FormControl({value: 'Nancy', disabled: true}, Validators.required), last: new FormControl('Drew', Validators.required) }); -- The suggestion cannot be implemented because the condition is not known at init time. "readonly" doesn't disable or renders the component readonly. Can you fix readonly (not disable that work) attribute on mdb-select ?

mdb2 pro answered 6 years ago

Dear Arek,

Disabling the controls renders it useless in a form so it looks like what I really want is to make autocomlete readonly.

Can you fix readonly attribute on mdb-select ?


mdb2 pro answered 6 years ago

Since we are talking about a form element, disabling it removes it from the form object but it should still be there.

What I need is to keep the form object intact but prevent the user from changing an <mdb-select> element, making it "readonly", just as "readonly" works on <input> elements. e.g., the user will not be able to open the dropdown nor change its value.

Today, setting [readonly]=true on an <mdb-select> has no effect.

 


Arkadiusz Idzikowski staff answered 6 years ago

Dear mdb2, I tried to recreate this problem in my app but without success. Could you provide demo and reproduction steps? Regards, Arek

mdb2 pro answered 6 years ago

Hi Arek, I sent you a demo using WeTransfer When you run it you will see the error in DevTools at runtime Look for
[disabled]="disableSelect"
in
<mdb-select formControlName="iBusinessType" id="iBusinessType" [options]="types" [disabled]="disableSelect"></mdb-select>

Arkadiusz Idzikowski staff answered 6 years ago

Thank you, we will take a look at it. For now please use 'disabled' option in form control.

mdb2 pro commented 6 years ago

Do you check it ?


Arkadiusz Idzikowski staff commented 6 years ago

If disable condition is not known at initialization, you can use reactive forms disable and enable methods.

For example:

this.yourFormGroupName.controls.yourFormControlName.disable();

this.yourFormGroupName.controls.yourFormControlName.enable();


Arkadiusz Idzikowski staff answered 6 years ago

Dear mdb2,

Could you describe in more details what is the exact problem with this attribute in mdb-select?

Regards,

Arek


mdb2 pro answered 6 years ago

Since we are talking about a form element, disabling it removes it from the form object but it should still be there.

What I need is to keep the form object intact but prevent the user from changing an <mdb-select> element, making it "readonly", just as "readonly" works on <input> elements. e.g., the user will not be able to open the dropdown nor change its value.

Today, setting [readonly]=true on an <mdb-select> has no effect.

 


mdb2 pro answered 6 years ago

Since we are talking about a form element, disabling it removes it from the form object but it should still be there.

What I need is to keep the form object intact but prevent the user from changing an <mdb-select> element, making it "readonly", just as "readonly" works on <input> elements. e.g., the user will not be able to open the dropdown nor change its value.

Today, setting [readonly]=true on an <mdb-select> has no effect.

 


mdb2 pro answered 6 years ago

Since we are talking about a form element, disabling it removes it from the form object but it should still be there.

What I need is to keep the form object intact but prevent the user from changing an <mdb-select> element, making it "readonly", just as "readonly" works on <input> elements. e.g., the user will not be able to open the dropdown nor change its value.

Today, setting [readonly]=true on an <mdb-select> has no effect.

 


mdb2 pro answered 6 years ago

Since we are talking about a form element, disabling it removes it from the form object but it should still be there.

What I need is to keep the form object intact but prevent the user from changing an <mdb-select> element, making it "readonly", just as "readonly" works on <input> elements. e.g., the user will not be able to open the dropdown nor change its value.

Today, setting [readonly]=true on an <mdb-select> has no effect.

 


Arkadiusz Idzikowski staff answered 6 years ago

Mdb-select was designed to work like native select which doesn't support readolny attribute. We will see if we can find a good solution for this problem. For now you can try those methods:

1. getRawValue provided by Reactive Forms

this.yourFormGroupName.getRawValue();

2. Block element with CSS

<div class="readonly-wrapper">

<mdb-select class="readonly-block" [options]="optionsSelect" formControlName="testSelect" placeholder="Choose your option"label="Example label"></mdb-select>

</div>
 
.readonly-wrapper {

cursor: not-allowed;

}

.readonly-wrapper .readonly-block {

pointer-events: none;

}

Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue
  • User: Pro
  • Premium support: No
  • Technology: MDB Angular
  • MDB Version: 6.2.3
  • Device: NA
  • Browser: NA
  • OS: NA
  • Provided sample code: No
  • Provided link: No
Tags