0

Hey i have input date picker from angular material and i try to make the value disable and let the user choose only from the date picker Calendar and it doesn't work for me.

This is my input:

<p class="p-rtl">
    <mat-form-field class="input-min" appearance="fill">
        <mat-label>Finish Date</mat-label>
        <input matInput [matDatepicker]="picker" placeholder="Choose a date" [formControl]="date">
        <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-datepicker #picker></mat-datepicker>
    </mat-form-field>
</p>

The problem is that the user can insert bad inputs and i want to block the user for this inputs.

I try to google it and read reference and nothing not work for me.

enter image description here

3
  • Just add "disabled" property to the input, <input disabled matInput [matDatepicker]="picker" placeholder="Choose a date" [formControl]="date"> Commented Oct 3, 2019 at 12:45
  • Its disable me the date picker calendar but the input its same available Commented Oct 3, 2019 at 12:48
  • @TheUnreal, a FormControl can be disabled, when is created, or using "disabled()" method or using a directive stackoverflow.com/questions/52619826/… Commented Oct 3, 2019 at 13:16

2 Answers 2

5

If you are trying to disable your input with disabled in HTML component you have a warning in console (reactive form).

To be able to avoid this, you can disable a FormControl with

 date: new FormControl({value: null, disabled: true})

Then to be sure that the user can't write anything you can put readonly attribute in the template

<p class="p-rtl">
    <mat-form-field class="input-min" appearance="fill">
        <mat-label>Finish Date</mat-label>
        <input matInput readonly [matDatepicker]="picker" placeholder="Choose a date" [formControl]="date">
        <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-datepicker #picker></mat-datepicker>
    </mat-form-field>
</p>
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of disabling the entire form you can disable like below

<p class="p-rtl">
    <mat-form-field class="input-min" appearance="fill">
        <mat-label>Finish Date</mat-label>
        <input matInput [matDatepicker]="picker" placeholder="Choose a date" [formControl]="date" disabled>
        <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-datepicker #picker  disabled="false"></mat-datepicker>
    </mat-form-field>
</p>

1 Comment

I am not at all sure that disabled="false" will work

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.