Search code examples
angularangular-material2angular-reactive-forms

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


Solution

  • 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