Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable (make read-only) text input on mat-datepicker when using a reactive form

I am using Angular Material 2's mat-datepicker and want the input element disabled so the user cannot edit the value using the text input.

There are detailed instructions in the Angular Material 2 docs on how to disable different parts of the mat-datepicker however, these do not seem to cover how to disable the text input when it is part of a reactive form.

The Angular Material docs suggest you disable the text input in the following way:

<mat-form-field>               // Add the disabled attribute to the input element ======               <input disabled                                                matInput                       [matDatepicker]="dateJoined"                       placeholder="Date joined"                       formControlName="dateJoined">               <mat-datepicker-toggle matSuffix [for]="dateJoined"></mat-datepicker-toggle>                // Add [disabled]=false to the mat-datepicker =======               <mat-datepicker [disabled]="false"                                startView="year"                                 #dateJoined></mat-datepicker>             </mat-form-field> 

However, if your datepicker is part of a reactive form the text element remains active and you get the following message from Angular:

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})});

I updated the FormGroup in the component to disable the FormControl, which has the desired effect of disabling the input, however, if you then get the value of the FormGroup using this.form.value the disabled form control is no longer present.

Is there a work around for this which does not involve having a separate template driven form using ngModel just for the mat-datepicker(s)?

EDIT: added “read-only” to post title to clarify this isn’t about disabling the control - because what I was asking about was to make the input element read-only, but not disable to control itself

like image 712
nclarx Avatar asked Nov 17 '17 00:11

nclarx


People also ask

How do I disable mat datepicker?

To disable only datepicker toggle we can add dispabled property to the mat-datepicker-toggle element. In this case user can only enter date by manually typing.

What is Mat datepicker toggle?

A datepicker is composed of a text input and a calendar pop-up, connected via the matDatepicker property on the text input. There is also an optional datepicker toggle button that gives the user an easy way to open the datepicker pop-up.

How do I change my mat datepicker language?

Angular material provides MAT_DATE_LOCALE whose value can be overridden in application module to change the default locale. To change locale at run time, we need to use setLocale method of DateAdapter . On this page we will provide complete example to set locale such as fr-FR, en-US and hi-IN for our Datepicker.


1 Answers

To create a disabled FormControl is really simple.

1 - Don't use disabled attribute in your template;

2 - Instantiate your FormGroup like this:

this.formGroup = this.formBuilder.group({   dateJoined: { disabled: true, value: '' }   // ... }); 

That being said, although you want to prevent users from typing something in the input, you still want to let them select a date by clicking the button (more specifically in matSuffix), right?

If it's correct, disable doesn't work for this case, because it'll disable all the input (including the button in matSuffix).

To solve your case, you can use readonly. Instantiate the FormGroup normally and then in template:

<input                             matInput    readonly <- HERE   [matDatepicker]="dateJoined"    placeholder="Date joined"    formControlName="dateJoined"> 

DEMO

like image 181
developer033 Avatar answered Oct 11 '22 16:10

developer033