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

Question

Question

LFForm object ChangeFieldSettings in Custom HTML field, does not save changes to repository

asked on June 12, 2023 Show version history

Hi, I have a custom HTML field that is updated after a lookup is used, which works well.

 

However, I have found that this does not seem to save the changes when the form is saved to the repository.

 

Is saving custom HTML field changes via the LFForm object, not supported for saving to the repository?

 

I also find that this is the same when using LFForm.ChangeFormSettings

0 0

Answer

SELECTED ANSWER
replied on June 13, 2023

It's expected that custom script changes don't save their changes into the actual form itself, and it applies for both classic and modern form.

If same form should be used for new submission and STR, we need another piece of script to be fired when form is read-only like this:

var form = Object.values(window.states)[0];
if (form.readonly) {
  updateForm();
}

 

3 0

Replies

replied on June 22, 2023

Just leaving a note in case anyone runs into this post, as it had me puzzled for a bit...

If you're using LFForm.getFieldValues to get the value from a date time field, it seems like they return two different objects based on whether the form is readonly or not.

live form

{dateStr: '29/06/2023', timeStr: '09:00 AM', momentObj: '2023-06-28T23:00:00.000Z'}

read only (ie. monitor page/repository/email task)

{dateStr: '29/06/2023', timeStr: '09:00 AM', dateTimeObj: '2023-06-28T23:00:00.000Z'}

Not sure if there are any others that we need to keep an eye out on, but this snippet seems to work nicely when converting it into a JavaScript date object:

const d = form.readonly ? new Date(validUntil.dateTimeObj) : new Date(validUntil.momentObj)

 

1 0
replied on June 12, 2023

Hi Jay, I tested on Forms 11.0.2212.30907 with the following custom script and confirmed it work when saved to repository:

LFForm.changeFieldSettings({fieldId: 1}, {label: "new label", description: "new text above"});

LFForm.changeFieldSettings({fieldId: 2}, {content: "new custom html content"});

Can you give more details like your custom script content? 

0 0
replied on June 12, 2023

Hi Rui,

My Forms is the self-hosted Laserfiche Forms Professional Version 11.0.2212.30987

See below for code:

const student = {
  preferred: null,
  surname: null,
  enrolYear: null,
  yearLevel: null,
}

const contact = {
  preferred: null,
  surname: null,
}

function updateForm() {
  student.preferred = LFForm.getFieldValues({ fieldId: 3 })
  student.surname = LFForm.getFieldValues({ fieldId: 4 })
  student.enrolYear = LFForm.getFieldValues({ fieldId: 5 })
  student.yearLevel = LFForm.getFieldValues({ fieldId: 6 })

  contact.preferred = LFForm.getFieldValues({ fieldId: 97 })
  contact.surname = LFForm.getFieldValues({ fieldId: 94 })

  LFForm.changeFormSettings({ title: `${student.enrolYear} Enrolment Offer`, description: `text, ${student.preferred} ${student.surname}, applying for year ${student.yearLevel} in ${student.enrolYear}.` })
  LFForm.changeFieldSettings({ fieldId: 99 }, { content: `<p>text, ${student.preferred} ${student.surname} text ${student.yearLevel} in ${student.enrolYear} text, ${contact.preferred + ' ' + contact.surname}.</p>
  <p>text</p>` })
}

LFForm.onLookupDone(updateForm, { lookupRuleId: 1 }) // after student data is loaded, we update the form

 

0 0
replied on June 13, 2023

The issue is that this is only triggering at the completion of the lookup.  But that won't happen on the archived version of the form.

You'll need a secondary method to trigger the updateForm() function when the form is archived.

Two options I can think of:

  1. Make a second copy of the form that is used for the archival, and modify the Javascript on that version to just call the function automatically.
  2. Check for a field that is populated by the lookup, and see if it already has a value, and if it does, call the function.
1 0
replied on June 13, 2023

I did some more testing and it looks like the changeFormSettings and changeFieldSettings  methods don't save their changes into the actual form itself, if used in another LFForm object's method (ie. onLookupDone or onFieldChange).

For now, I am leaving the Javascript to change data on the frontend of the form for the viewer.

For saving into the repository, I have just copy and pasted into the Custom HTML field, what I am filling it with, replacing the JS variables with LF tokens instead (which seems to work ok).

It would be great if it could be setup in a way where I didn't have to manage the text and variables in two places though.

 

0 0
SELECTED ANSWER
replied on June 13, 2023

It's expected that custom script changes don't save their changes into the actual form itself, and it applies for both classic and modern form.

If same form should be used for new submission and STR, we need another piece of script to be fired when form is read-only like this:

var form = Object.values(window.states)[0];
if (form.readonly) {
  updateForm();
}

 

3 0
replied on June 13, 2023

Thank you, it looks like that is the missing piece I needed to make this apply the changes to the actual form, which works great now.

It would be nice if in the future, we could have an additional property in changeFieldSettings and changeFormSettings to incorporate this easier.

Something along the lines as follows would be appreciated;

  LFForm.changeFieldSettings({ fieldId: 1 }, {content: 'my content', permanent: true}

 

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.