angular / components

Component infrastructure and Material Design components for Angular
https://material.angular.io
MIT License
24.4k stars 6.76k forks source link

Datepicker required validation is wrong #16761

Open micobarac opened 5 years ago

micobarac commented 5 years ago

Reproduction

StackBlitz example:

https://stackblitz.com/edit/angular-x2g8lj?file=app%2Fdatepicker-overview-example.ts

Steps to reproduce:

  1. Enter any invalid date value, like jjj
  2. date.errors.required is always true for any invalid, non-empty value

Expected Behavior

Like with any other Angular Material component, using Validators.Required, date.errors.required should be true ONLY when the datepicker value is EMPTY!

  <mat-form-field>
    <input matInput [matDatepicker]="picker" placeholder="Choose a date" required formControlName="date">
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
  </mat-form-field>
    this.form = this.formBuilder.group({
      date: ['', Validators.required]
    });

So, for any non-empty invalid value, instead of:

  <small *ngIf="date.errors?.required && !date.errors?.matDatepickerParse && date.touched" class="mat-warn">Date is required
  </small>
  <small *ngIf="date.errors?.matDatepickerParse && date.touched" class="mat-warn">
    Date is invalid
  </small>

there should be:

  <small *ngIf="date.errors?.required && date.touched" class="mat-warn">Date is required
  </small>
  <small *ngIf="date.errors?.matDatepickerParse && date.touched" class="mat-warn">
    Date is invalid
  </small>

Also, instead of:

{ "required": true, "matDatepickerParse": { "text": "jjj" } }

there should be:

{ "matDatepickerParse": { "text": "jjj" } }

as a value of date.errors object.

matDatepickerParse is a strange concept. Using a custom datepicker validator should be allowed instead of it.

Actual Behavior

For invalid non-empty values, this is the control errors object:

{ "required": true, "matDatepickerParse": { "text": "jjj" } }

Environment

Angular CLI: 8.2.0
Node: 12.8.0
OS: darwin x64
Angular: 8.2.0
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.802.0
@angular-devkit/build-angular     0.802.0
@angular-devkit/build-optimizer   0.802.0
@angular-devkit/build-webpack     0.802.0
@angular-devkit/core              8.2.0
@angular-devkit/schematics        8.2.0
@angular/cdk                      8.1.2
@angular/flex-layout              8.0.0-beta.26
@angular/material                 8.1.2
@ngtools/webpack                  8.2.0
@schematics/angular               8.2.0
@schematics/update                0.802.0
rxjs                              6.5.2
typescript                        3.5.3
webpack                           4.38.0
FaisalAfroz commented 2 years ago

Any update on this issue?

heyaheyaho commented 1 year ago

It doesn't look difficult to solve.

chitgoks commented 1 year ago

Still hope this gets resolved. It looks ugly saying it is required when there is something in the input mat-datepicker.

jota0222 commented 8 months ago

It seems this is still an issue. It's showing an error saying my field is required when it just have the wrong format.