matDatepickerFilter - not validating startDate and endDate as expected after removing .bind(this)
I had an issue of no date clickable and followed this https://github.com/angular/components/issues/21929 to resolve it. But my problem is that the filter isn't always checking my validations.
Expected Behavior : endDate should always be greater than or equal to startDate and the dates of the calendar should be clickable if even though manual typing input is allowed.
Actual Behavior : matDatepickerFilter="boundStartDate" isn't always checking my validations inside boundStartDate after first time. It validates well on the first attempt, but once I go to the next page of the reactive form, come back to the first page and then again try to edit dates,it does not check my condition until I click on endDate field. without clicking on endDate, it will take me to the next screen without validating endDate should be greater than equal to startDate.
Scenario:
- Open the form, set the dates and go the next screen of the form. (Everything works as expected)
- Come back to screen 1, try to edit just the startDate and clicking on next is taking me to the next screen without checking the condition. (startDate should be less than or equal to endDate)
- It will only check my condition only when I click endDate field else it goes to next screen of the form without validating.
.html
<mat-form-field>
<mat-label>Start date</mat-label>
<input matInput [matDatepicker]="this.startdate" formControlName="StartDt"
id="cp.startDt" required>
<mat-datepicker-toggle matSuffix [for]="startdate"></mat-datepicker-toggle>
<mat-datepicker #startdate ></mat-datepicker>
</mat-form-field>
<mat-form-field>
<mat-label>End date</mat-label>
<input matInput [matDatepicker]="enddate" [matDatepickerFilter]="boundStartDate" [min]="minDate" formControlName="endDt"
id="cp.endDt" required>
<mat-datepicker-toggle matSuffix [for]="enddate"></mat-datepicker-toggle>
<mat-datepicker #enddate></mat-datepicker>
<mat-error *ngIf="!result">End Date should be greater than Start Date</mat-error>
</mat-form-field>
.ts
boundStartDate : (date: Date | null) => boolean =
(date: Date | null) => {
this.minDate = new Date (this.formatedStartDate);
if(new Date(this.formatedStartDate) <= new Date(this.formatedEndDate)){
return true;
} else {
return this.result;
}
}
I suspect that it's because you're re-assigning the minDate inside of the boundStartDate function. The filter function isn't really meant to have side effects so it won't re-run change detection if something changes inside of it.
I suspect that it's because you're re-assigning the
minDateinside of theboundStartDatefunction. The filter function isn't really meant to have side effects so it won't re-run change detection if something changes inside of it.
@crisbeto I tried revmoing that, but no luck. and BTW if I remove that everytime I try to select the endDate from Calendar, it will show all the dates from previous startDate.
Do you want me to try anything else. This has been killing me since 2 days.
It's a little difficult to see what might be wrong going based purely off a code snippet. Can you put together a Stackblitz that shows the failure? You can click on "Open in Stackblitz" on one of the examples from the docs to get everything set up: https://material.angular.io/components/datepicker/examples
It's a little difficult to see what might be wrong going based purely off a code snippet. Can you put together a Stackblitz that shows the failure? You can click on "Open in Stackblitz" on one of the examples from the docs to get everything set up: https://material.angular.io/components/datepicker/examples
Sure I will check the docs and I will put that code in Stackblitz for you to check.
Closing this issue due to no recent activity, if this is still an issue please create a new issue with a reproduction.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.