I have a template driven form with a read only input. When the user clicks on the input, a date picker appears. After the user selects the date, the input field gets populated with the information. However, when I click submit, the data is not there from that input. The other fields work though. So in this case, I get an output of
Console Output:
Object {refNumber: "215170", date: ""}
HTML code:
<form [formGroup]="myForm" novalidate (ngSubmit)="onSubmit(myForm)">
<div class="form-group">
<label>Reference Number</label>
<input type="text" class="form-control" formControlName="refNumber">
</div>
<!--Date Picker-->
<input (focus)="toggleDatePicker(true)" readonly value={{date}} class="form-control" formControlName="date" />
<date-picker *ngIf="showDatePicker" [initDate]="date" (onDatePickerCancel)="toggleDatePicker($event)" (onSelectDate)="setDate($event)"
ngDefaultControl></date-picker>
</form>
Component Code
export class AppComponent implements OnInit {
public myForm: FormGroup;
constructor(private _fb: FormBuilder) { }
ngOnInit() {
this.myForm = this._fb.group({
refNumber: ['', [Validators.required, Validators.minLength(5)]],
date: [''],
});
}
onSubmit(formOutput: FormGroup) {
console.log(formOutput.value);
alert('Submit this data to database')
}
// Date TimePicker
private date: any;
private showDatePicker: boolean;
// Date Pciker
toggleDatePicker(status: boolean): void {
this.showDatePicker = status;
}
setDate(date: any): void {
this.date = date;
}
}
----------Update-------------------------- I have created a new project just to replicate this issue. The problem is still there. Here is a video of what is happening in this project. To me, it looks like the input value only gets updated when I type something in. Any hard-coded value or in this case, the date that gets passed in, does not update the actual input value behind the scene?