最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get value out of mat-datepicker in FormControl - Stack Overflow

programmeradmin1浏览0评论

I am creating a POST request to an API, and one of the fields which throws an error is a mat-datepicker field, since it's inside the ngOnInit() call(and nothing is yet selected, I guess). Fields such as name, email etc. behave well, but I'm having trouble getting a value out of the datepicker field... How could I send that value as well when submitting the form?

My attempt was this:

ngOnInit() {
  this.myForm = new FormGroup({
    date_of_birth: new FormControl(new Date().toISOString(), [
       Validators.required,
       this.backendValidation.bind(this, 'date_of_birth')
    ])
   })
  }


<mat-form-field>
  <mat-label>Date</mat-label>
  <input matInput [matDatepicker]="date_of_birth" formControlName="date_of_birth" />
  <mat-datepicker-toggle matSuffix [for]="date_of_birth"></mat-datepicker-toggle>
  <mat-datepicker #date_of_birth></mat-datepicker>
</mat-form-field>

I am creating a POST request to an API, and one of the fields which throws an error is a mat-datepicker field, since it's inside the ngOnInit() call(and nothing is yet selected, I guess). Fields such as name, email etc. behave well, but I'm having trouble getting a value out of the datepicker field... How could I send that value as well when submitting the form?

My attempt was this:

ngOnInit() {
  this.myForm = new FormGroup({
    date_of_birth: new FormControl(new Date().toISOString(), [
       Validators.required,
       this.backendValidation.bind(this, 'date_of_birth')
    ])
   })
  }


<mat-form-field>
  <mat-label>Date</mat-label>
  <input matInput [matDatepicker]="date_of_birth" formControlName="date_of_birth" />
  <mat-datepicker-toggle matSuffix [for]="date_of_birth"></mat-datepicker-toggle>
  <mat-datepicker #date_of_birth></mat-datepicker>
</mat-form-field>
Share Improve this question edited Mar 30, 2020 at 12:00 Smithy asked Mar 30, 2020 at 11:27 SmithySmithy 8476 gold badges19 silver badges51 bronze badges 2
  • the value must be an object date – Eliseo Commented Mar 30, 2020 at 11:53
  • @Eliseo could you please show how could I implement that? – Smithy Commented Mar 30, 2020 at 11:57
Add a ment  | 

1 Answer 1

Reset to default 5

In HTML:

<form [formGroup]="empForm" (ngSubmit)="onSubmit()">
<mat-form-field>
    Date
    <input matInput [matDatepicker]="picker" formControlName="date_of_birth" />
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
</mat-form-field>
</form>

<button (click)="get()">get</button>

TS:

this.empForm = this.fb.group({
  date_of_birth: new FormControl(new Date(), [
    Validators.required
  ])
});

To console:

get(){
 console.log(this.empForm.get('date_of_birth').value);
}

Hope this will help you. If any issues, then I think its problem with the custom validator you have - this.backendValidation.bind(this, 'date_of_birth').

Hope this works !

发布评论

评论列表(0)

  1. 暂无评论