Hey everyone,
I have a use case where I have a form to create a record in the database, and I want to be able to 'load' data for existing records into the form so that users can edit records.
I have a query which returns the relevant data, however when I try and use the form.setData() method, it doesn't behave as I would expect.
Example of the data
const data = {
id: 16,
uuid: "908b8d75-6d55-409b-b6bc-ba19d6e0c7a3",
group_name: "Express",
group_type: "Roller Blinds",
max_width: "2700",
max_width_center: "",
min_width: "300",
max_drop: "2900",
min_drop: "500",
styles: [ "a4e24b9f-3791-4178-ab61-533ae6b289da", "f763fbc8-98eb-47f5-9883-9d0ed31555d0" ],
product_type: "Blinds",
width_note: "",
drop_note: "",
depth: "",
material: "",
tracking: "",
brackets: "",
operation: ""
}
Upon using formComponent.setData(data), I can see in the State section of the menu that the initialData properties are being set, however, the values do not appear in the form fields.
I did think of a workaround where I can have a variable for each of the input fields, and then loop over those and use the component.setValue() method to add the values that way.
I am also unable to get this to work, I did this with the first input field(product_type) to validate my assumptions, however it looks like the value is being set, then removed.
Here is the code I am using to manually set the values for each input
section_CreateProductGroupForm.clear();
const productDataToLoad = query_ProductGroupsList.data[i];
// This would be my preferred method of setting the values
// section_CreateProductGroupForm.setData(productDataToLoad);
// The workaround for this will be manually adding each of the values to the form
const inputFields = {
'product_type': component_CreateProductGroup_ProductTypeInput
};
Object.keys(inputFields).forEach(fieldKey => {
const element = inputFields[fieldKey];
const value = productDataToLoad[fieldKey];
element.setValue(value);
})
component_CreateProductGroup_ProductTypeInput.setValue(productDataToLoad.product_type);
section_CreateProductGroupModal.open();
If anyone has had experience with fixing an issue like this, I would love to get your insight.
Thankyou!