String array type for multivalued properties

Recently I’ve been working with Touch UI multifield widget (granite/ui/components/foundation/form/multifield) on one of my recent projects, and I’ve found out the following:

  • If for the first time an editor enters just one entry and saves it, AEM will create a property of type String on the corresponding node and store the value of the entry in it. Subsequent additions of entries will not change the property type – it will be of the type String and the new values will be appended. All values will be comma separated.
  • If for the first time the user enters more than one entry and saves them, AEM will create a property of type String[]

I’ve been investigating this ‘problem’ and according to the info found on Internet, it seems that’s the way how it works (the desired AEM behavior).

After several tries I’ve found out some partial solution by using Sling @TypeHint. Here’s the example (only part of a component dialog is shown):

<references
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/multifield"
class="foundation-layout-util-maximized-alt long-label"
cq:showOnCreate="{Boolean}true"
fieldLabel="References">
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/fieldset"
name="./references">
<layout
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/layouts/fixedcolumns"
method="absolute"/>
<items jcr:primaryType="nt:unstructured">
<column
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/container">
<items jcr:primaryType="nt:unstructured">
<reference
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/pathbrowser"
fieldLabel="Reference"
name="./reference"
rootPath="/content"/>
</items>
</column>
</items>
</field>
</references>
<references-typehint
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/hidden"
name="./references@TypeHint"
value="String[]"/>

On this way regardless of the number of entries entered by an editor, AEM will always create a property of type String[]. This simplifies a logic in Sling model because property is always of the same type. Unfortunately, no possibility to enforce AEM to store everything (comma separated) in a property of type String.

If you would like to try out this patch in your project, do the following:

  • make the necessary changes in a component dialog and deploy the change
  • remove the corresponding property in JCR before saving any entries