Input fields that are populated programmatically are not saved into collection

Question:
When I’m populating form fields programmatically they are not saved into collection.
What I’m missing?

import { currentMember } from 'wix-members-frontend';
$w.onReady(function () {
    prepopulateForm();
});
async function prepopulateForm() {
    const member = await currentMember.getMember();
    console.log("member", member);
    if (!member) return;
    $w('#email').value = member.loginEmail;
}

What are you trying to achieve:
I would like to save the value of email field populated programmatically from currentMember object to my collection. The email value is successfully displayed but not being saved. The field is connected to collection.

Additional information:
When updated manually the data is saved to connection.

Thanks!

you can use the setFieldValue function of the form’s dataset to programmatically set the value of a field. This function triggers the form’s data binding, so the value will be saved to the collection when the form is submitted.

import { currentMember } from 'wix-members-frontend';

$w.onReady(function () {
    prepopulateForm();
});

async function prepopulateForm() {
    const member = await currentMember.getMember();
    console.log("member", member);
    if (!member) return;
    $w('#dataset1').setFieldValue('email', member.loginEmail); // replace 'dataset1' with the ID of your form's dataset and 'email' with the field key in your collection
}

Thanks, now it works!

1 Like