【问题标题】:Angular Validation: Mat-Error doesn't go away after correcting the input角度验证:修正输入后 Mat-Error 不会消失
【发布时间】:2021-12-13 09:39:48
【问题描述】:

我实现了一个日期选择器,只要日期无效,它就会显示一个垫子错误。问题是,除非用户单击日历然后选择正确的日期,否则在更正日期后错误不会消失。

HTML:

<mat-form-field [appearance]="appearance">
  <input
    matInput
    class="om-text-input"
    type="text"
    placeholder="DD.MM.YYYY"
    [required]="isRequired"
    [min]="minDate"
    [max]="maxDate"
    (focus)="Datepicker.open()"
    [matDatepicker]="Datepicker"
    #input="ngModel"
    [(ngModel)]="date"
  />
  <mat-datepicker-toggle matSuffix [for]="Datepicker">
    <mat-icon matDatepickerToggleIcon>calendar_today</mat-icon>
  </mat-datepicker-toggle>
  <mat-datepicker #Datepicker>
    <mat-datepicker-actions>
      <button mat-button matDatepickerCancel>Cancel</button>
      <button mat-raised-button color="primary" matDatepickerApply>Apply</button>
    </mat-datepicker-actions>
  </mat-datepicker>
  <mat-error class="om-error" *ngIf="input.hasError">Invalid date</mat-error>
</mat-form-field>

组件:

 export class DatePickerComponent {
      date: Date = new Date();
      minDate = DateTime.now().minus({ days: 365 }).toISODate();
      maxDate = DateTime.now().plus({ days: 365 }).toISODate();
    
      @Input()
  isRequired = false;

  @Input()
  appearance: Appearance = Appearance.outline;
}

模块:

providers: [{ provide: MAT_DATE_LOCALE, useValue: 'DE-EU' }],

【问题讨论】:

  • 从日期选择器中选择时,日期值格式是什么?和'28.10.2021'一样吗?

标签: angular typescript validation angular-material datepicker


【解决方案1】:

如果您使用MatNativeDateModule,则使用Date.parse。格式不可配置。当它无法解析文本时,它会设置一个 Invalid Date 作为值,因此是无效的。

如果你想支持这种格式,你需要使用另一个支持你的格式的date adapter。支持custom formatMomentDateModule的材料发货。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多