You are viewing limited content. For full access, please sign in.

Question

Question

Set Time field through LFForm.setFieldValues throws error on submit

asked on January 6 Show version history

I am on Laserfiche Forms Professional Version 12.0.2509.20409 using the modern designer and when I try to set a time field value through JavaScript, it looks good on the form, but when it is submitted, I get the below error:

Errors encountered during forms submission:
Field Name: Time, Position: 0, Error: This value does not have a proper time format. [LFF9305-InvalidTimeFormat] [LFF9312-ErrorOccuredDuringFormsValidation]
[LFF9320-FormsValidationAggregateException]

 

For troubleshooting, I have a form with only a time field.  I then have the following JavaScript:

function logTime(){
  console.log('Starting Log');
  let timeVal = LFForm.getFieldValues({fieldId: 1});
  console.log("timeVal: " + timeVal);
  const timeJSON = JSON.stringify(timeVal);
  console.log("timeJSON: " + timeJSON);
  console.log('Ending Log');
}
console.log('Starting Form');
LFForm.setFieldValues({ fieldId: 1 }, {timeStr: '7:44:46 AM', dateTimeObj:'2026-01-06T12:44:46.000Z'});
LFForm.onFieldChange(() => logTime(), {fieldId: 1});
console.log('Ending Form');

I know that the documentation says that a time field only takes a timestr value/property and I have tried to set it with and without the dateTimeObj value/property and get the same thing.  If I manually set the time value through the form, then it submits without error.

I have found that when I get the field value after the field has changed, it includes the dateTimeObj value/property in the returned object.  I then found that the dateTimeObj value/property is different when set though code than when it is set manually through the field on the form.  Below is from the DevTools console and it shows both when the field was set through code and then again when I manually changed it.  Notice that when I set it with code, the date is 1/1/2000, but when I set it through the field, the date is 1/1 of current year.

0 0

Answer

SELECTED ANSWER
replied on January 7 Show version history

I'm not sure what the expected value type is supposed to be. It is odd that you need the timeStr but you can't submit the form if the field doesn't have the dateTimeObj (which isn't constructed internally when given just the timeStr.

This works though:

const getTimeValue = (hours, minutes, seconds, amPm) => {
    d = new Date();
    let hours24 = hours === 12 && amPm.toLowerCase() === 'am' ? 0 : 12;
    hours24 = amPm.toLowerCase() === 'am' ? hours : hours + 12;
    d.setHours(hours24);
    d.setMinutes(minutes);
    d.setSeconds(seconds);
    d.setMilliseconds(0);
   
    return { 
        timeStr: d.toLocaleString('en-US', { hour: 'numeric', minute: '2-digit', second: '2-digit', hour12: true }),
        dateTimeObj: d.toString()
     };
}

LFForm.setFieldValues({ fieldId: 21 }, getTimeValue(10, 10, 10, 'am'))

 

0 0
replied on January 7

@████████

I do not know what was wrong with my timeStr formatting, but it would not submit even with the dateTimeObj.  I was just adding that because it was being returned when I get the value of the time field after it is changed.

 

Interestingly enough, the returned value from getTimeValue also includes the dateTimeObj but formatted differently than what is returned from the field after being set.

I added the following to my test project:

const getTimeValue = (hours, minutes, seconds, amPm) => {
    d = new Date();
    const hours24 = amPm.toLowerCase() === 'am' ? hours : hours + 12;
    d.setHours(hours24);
    d.setMinutes(minutes);
    d.setSeconds(seconds);
    d.setMilliseconds(0);
   
    return { 
        timeStr: d.toLocaleString('en-US', { hour: 'numeric', minute: '2-digit', second: '2-digit', hour12: true }),
        dateTimeObj: d.toString()
     };
}

let timeValue = getTimeValue(7,44,46,'AM');
let timeValueJSON = JSON.stringify(timeValue);
console.log("timeValueJSON: " + timeValueJSON);

And here is the returned value from getTimeValue:

0 0
replied on January 7

the datetime obj one just needs to be a string that can be used to construct a date with new date(d.toString()). There are a few supported strings/numbers that can be used here. The date itself should be irrelevant to your use case since there is no date associated with the time field. 

You could update the getTimeValue function to support a date as well so you could use it for date, time, and datetime fields all in one

0 0
replied on January 7

I also just fixed my code example to handle 12am = 0 in the 24h time needs.

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.