From DAYCARE:
I'd like to provide you a quick summary of the resolution of this issue :
Problem : Multifield with one entry saves data in non array attribute
Root cause : Default behavior, but can be overridden using a typeHint
Solution : Used typeHint on multifield node with proper field name & type parameter syntax (fieldName@String[])
References:
Apache Sling :: Manipulating Content - The SlingPostServlet (servlets.post)
Extending Page Properties in AEM Sites
Sample Code from DayCare with relevant code in bold.
The key was understanding that the multifield needed the typeHint and it does not use the dot (.) symbol like the nested field does.
This quote is taken from the first article referenced above:
"If the @TypeHint value ends with [], it indicates a multi-value property. A multi-value property is usually auto-detected if there are multiple values for the property (i.e. request parameter). But if only a single value is present in the request, the desired property type needs to be explicitly defined as multi-value by stating @TypeHint=<type>[]."
So most examples showed the typeHint being used within an attribute such as name="width@TypeHint
but in this case, since we want to force it to an array even when there is only one element, the attribute typeHint needed to be used.
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="nt:unstructured">
<content jcr:primaryType="nt:unstructured">
<items jcr:primaryType="nt:unstructured">
<tabs jcr:primaryType="nt:unstructured">
<items jcr:primaryType="nt:unstructured">
<cug
jcr:primaryType="nt:unstructured"
jcr:title="CUG"
sling:orderBefore="advanced"
sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns">
<items jcr:primaryType="nt:unstructured">
<column
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container">
<items jcr:primaryType="nt:unstructured">
<cugPrincipals
granite:class="foundation-layout-util-maximized-alt"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
fieldDescription="Click 'Add Field' to add a CUG Permission."
fieldLabel="Access is restricted to the following groups:"
required="{Boolean}true"
typeHint="cq:cugPrincipals@String[]">
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
name="./cq:cugPrincipals"/>
</cugPrincipals>
</items>
</column>
</items>
</cug>
</items>
</tabs>
</items>
</content>
</jcr:root>