Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datepicker required validation is wrong #16761

Open
micobarac opened this issue Aug 13, 2019 · 4 comments
Open

Datepicker required validation is wrong #16761

micobarac opened this issue Aug 13, 2019 · 4 comments
Labels
area: material/datepicker P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@micobarac
Copy link

micobarac commented Aug 13, 2019

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
@micobarac micobarac changed the title Datepicker required validation is a huge mess Datepicker required validation is wrong Aug 13, 2019
@andrewseguin andrewseguin added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Aug 13, 2019
@FaisalAfroz
Copy link

Any update on this issue?

@heyaheyaho
Copy link

It doesn't look difficult to solve.

@chitgoks
Copy link

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

@jota0222
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/datepicker P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

No branches or pull requests

7 participants