2

In Angular 13 application, using PrimeNG Calendar

<p-calendar [minDate]="today" [showIcon]="true" styleClass="form-control" showButtonBar="true"
                                dateFormat="dd/mm/yy" inputId="icon" placeholder="dd/mm/yy"
                                formControlName="eventStartDate"></p-calendar>

using in a Reactive form, it is working fine while save new form.same form I am using for edit also. To edit the form loading the data based on code. But while patchValue to the form, getting the error ERROR Unexpected literal at position 2 enter image description here

the edit code

loadEventToEdit() {
    this.eventService.getEventByCode(this.eventCode).subscribe((res) => {
      this.event = res;
      console.log('the event to update', this.event);

      this.theForm.patchValue(res);

Rest of fileds path value is working fine.

Manu
  • 1,243
  • 5
  • 17
  • 43
  • I see the date formats are different, in `eventStartDate` are you sure this is expected? – Deepak Jha Dec 19 '21 at 14:40
  • Hey Deepak, yea in DB it is storing as yy--mm-dd. also, in p-calendar I changed the format to dateFormat="yy/mm/dd", but error – Manu Dec 19 '21 at 15:20

5 Answers5

2

Before patchValue set the date as new Date.

loadEventToEdit() {
    this.eventService.getEventByCode(this.eventCode).subscribe((res) => {
      this.event = res;
      console.log('the event to update', this.event);

      this.event.timing.eventStartDate = new Date(
        this.event.timing.eventStartDate
      );

      this.theForm.patchValue(this.event);
Manu
  • 1,243
  • 5
  • 17
  • 43
0

Give complete way to your res where res have to patched. For FormArray you have to make getter first and the can you use Push or insert method to patch with form array

 loadEventToEdit() {
        this.eventService.getEventByCode(this.eventCode).subscribe((res) => {
          this.event = res;
         this.theForm.controls['eventStartDate'].patchValue(this.event.timing.eventStartDate);
    })
    }
Tauseef Arshad
  • 583
  • 5
  • 13
0

I had similar error. Make sure having field type as Date and set it as new Date before patchValue on form.

Bozy
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Miguel Jan 12 '22 at 20:40
0

One way is to use DayJS.

let dates = [
  dayjs(feature?.start_date, 'MM-DD-YYYY').toDate(),
  dayjs(feature?.end_date, 'MM-DD-YYYY').toDate(),
];

this.form.patchValue(
  {
    dateRange: dates || null
  },
  {
    emitEvent: false,
    onlySelf: true,
  }
);
Ben Racicot
  • 5,332
  • 12
  • 66
  • 130
0

The P-Calendar component needs the Date object as a value, you cannot pass it to other types of objects. Therefore, when patching, be sure to access the form's controls and create it with a date object.

response => {
   this.yourForm.controls['likeStartDate'].patchValue(new Date(response.startDate));         }