How do we store multifields with more than one field as array in AEM 6.5?
How do we store multifield that has more than one field as array in AEM 6.5 as acs-commons-nested property is deprecated in 6.5?
How do we store multifield that has more than one field as array in AEM 6.5 as acs-commons-nested property is deprecated in 6.5?
Hi @chinmayish
If you have a component that uses both Classic UI and Touch UI dialogs, and you want to ensure that the multifield data is synchronized between the two dialogs, you will need to implement some custom logic.
Here's an approach you can take to handle this scenario:
1. In your Classic UI dialog, add a hidden field to store the multifield data as JSON.
<mycomponent
jcr:primaryType="cq:Widget"
xtype="dialog">
<items jcr:primaryType="cq:WidgetCollection">
<!-- Other fields -->
<myMultifieldData
jcr:primaryType="cq:Widget"
xtype="hidden"
name="./myMultifieldData"/>
</items>
</mycomponent>
2. In your Classic UI JSP, retrieve the multifield data from the hidden field and convert it to a JSON object.
<%
String multifieldData = properties.get("myMultifieldData", "");
JSONArray jsonArray = new JSONArray(multifieldData);
%>
3. Iterate over the JSON array and render the multifield data in your Classic UI JSP.
<%
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String field1Value = jsonObject.getString("field1");
String field2Value = jsonObject.getString("field2");
// Render the fields as needed
}
%>
4. In your Touch UI dialog, use the Coral 3 multifield component as described in the previous answer.
5. In your Touch UI dialog's client-side JS, populate the Classic UI hidden field with the multifield data.
(function($, $document) {
"use strict";
$document.on("dialog-ready", function() {
var $multifield = $("coral-multifield[name='./myMultifield']");
$multifield.on("change", function() {
var multifieldData = [];
$multifield.find("coral-multifield-item").each(function() {
var $item = $(this);
var field1Value = $item.find("coral-autocomplete[name='./field1']").val();
var field2Value = $item.find("coral-textfield[name='./field2']").val();
multifieldData.push({
field1: field1Value,
field2: field2Value
});
});
// Store the multifield data in the Classic UI hidden field
$("input[name='./myMultifieldData']").val(JSON.stringify(multifieldData));
});
});
})(jQuery, jQuery(document));
By populating the Classic UI hidden field with the multifield data from the Touch UI dialog, you ensure that the data is synchronized between the two dialogs. This way, when the data is submitted from the Classic UI dialog, it will be rendered correctly.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.