Angular 6反应形式的预填充和验证日期 [英] Pre-populating and validating date in Angular 6 reactive form

查看:129
本文介绍了Angular 6反应形式的预填充和验证日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除日期外,所有其他字段均已预先填充.

All the rest of the fields are getting pre-populated except date for me.

出于测试目的,我将日期硬编码为MM/DD/YYYY.

For testing purposes, i have hard coded the date as MM/DD/YYYY.

从数据库中,我将获得日期和时间,因此我需要使用管道将其设置为MM/DD/YYYY(我对此是否正确?)

From the DB, i'll get Date and Time, so i'll need to use the pipe to make it MM/DD/YYYY (am i correct about this?)

组件代码

this.projectForm = new FormGroup({
  'name': new FormControl(project.ProjectName, [Validators.required, Validators.minLength(3), Validators.maxLength(20)]),
  'customerName': new FormControl(project.CustomerName, [Validators.required, Validators.minLength(3), Validators.maxLength(20)]),
  'soNo': new FormControl(project.CustomerOrderId, [Validators.required, Validators.maxLength(30)]),

  'poNo': new FormControl({ value: project.PurchaseOrderId, disabled: true }),

  'expectedDate': new FormControl({ value: project.ExpectedDate, disabled: true }), 
  'installDate': new FormControl(project.InstallDate),
  'closeDate': new FormControl(project.CloseDate),

  'status': new FormControl(project.Status, [Validators.required, ForbiddenProjectStatusValidator.forbiddenProjectStatus])
});


//setting the dates and status dropdown
this.projectForm.patchValue({
  'expectedDate': '08/17/2018',
  'installDate': '08/18/2018',
  'closeDate': '08/19/2018',
  'status': project.Status ? project.Status : "" 
});

html

<input type="date" id="expectedDate" class="form-control" placeholder="Expected Date" formControlName="expectedDate">

由于输入日期类型,浏览器显示日历控件.

Due to input type date, browser shows calendar control.

所以基本上,

操作方法

  • 预填充日期
  • 确认日期为MM/dd/yyyy(不需要任何日期).用户可能无法输入或选择完整日期

更新1:

选择的答案是完美的,它详细说明了片刻的用法,但现在我已经采用了最简单的解决方案...

selected answer is perfect, it details the use of moment but for now i have gone with simplest solution...

https://css-tricks.com/prefilling-date-input/

如何将当前日期转换为角度2的YYYY-MM-DD格式

如何格式化JavaScript日期

这就是对我有用的

expectedDate: new Date('08/08/2018').toISOString().substr(0, 10)

或当前为

expectedDate: new Date(new Date().toLocaleDateString("en-US")).toISOString().substr(0, 10) 

expectedDate: '2018-08-08' 

日期必须为YYYY-MM-DD.

the date has to be YYYY-MM-DD.

为了验证,模式有效

'expectedDate': new FormControl(project.InstallDate, [Validators.pattern('[0-9]{4}-[0-9]{2}-[0-9]{2}')])

推荐答案

您可以定义日期的自定义验证器.使用支持日期验证的日期时间,例如moment

You can define a custom validator for date. Use date time that support date validation such as moment

import { FormControl } from "@angular/forms";
import * as moment from "moment";

export function DateValidator(format = "MM/dd/YYYY"): any {
  return (control: FormControl): { [key: string]: any } => {
    const val = moment(control.value, format, true);

    if (!val.isValid()) {
      return { invalidDate: true };
    }

    return null;
  };
}

然后在表单控件中使用它

Then use it in form control

{
   ...
  'closeDate': new FormControl(project.CloseDate, [ DateValidator() ]),
}

从数据库中,我将获得日期和时间,因此我需要使用管道将其设置为MM/DD/YYYY(我对此是否正确?)

From the DB, i'll get Date and Time, so i'll need to use the pipe to make it MM/DD/YYYY (am i correct about this?)

您不能在FormControl中使用管道.最简单的方法是在修补值以形成

You can't use pipe in FormControl. The simplest way is converting to target format before patching value to form

this.projectForm.patchValue({
  'expectedDate': moment(model.expectedDate).format('MM/dd/YYYY'),
  ...
});

这篇关于Angular 6反应形式的预填充和验证日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆