2
votes

I've created an AEM Touch UI multifield in a component dialog node. It has a sub-field of select. The select control contains a list of permissions. The idea is that an author can select multiple permissions and the user must have at least one of those permissions in order to see the component, or if no permissions are specified for that component, all users will see it. Below is the .content.xml file for the dialog tab (the tab gets included via granite/ui/components/foundation/include reference).

I can add new permissions to the multifield, change them, and remove them with one caveat: I can't remove the last permission in the list. For some reason, AEM isn't allowing me to have an empty multifield once I've selected something. I've tried setting allowBlank to true, but I don't think this property applies to the Touch UI--either way, it doesn't fix the issue.

How can I allow a content author to remove all items in the multifield?

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/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"
    jcr:title="Portal Filters"
    sling:resourceType="granite/ui/components/foundation/container">
    <items jcr:primaryType="nt:unstructured">
        <permissions
            jcr:primaryType="nt:unstructured"
            sling:resourceType="granite/ui/components/foundation/form/multifield"
            fieldDescription="A user must have at least one of these permissions to view this component"
            fieldLabel="Permissions">
            <field
                jcr:primaryType="nt:unstructured"
                sling:resourceType="granite/ui/components/foundation/form/select"
                name="./permissions">
                <datasource
                    jcr:primaryType="nt:unstructured"
                    sling:resourceType="/apps/mportal/datasources/permissions"/>
            </field>
        </permissions>
        <missions
            jcr:primaryType="nt:unstructured"
            sling:resourceType="granite/ui/components/foundation/form/multifield"
            fieldDescription="A user must have at least one of these missions to view this component"
            fieldLabel="Missions">
            <field
                jcr:primaryType="nt:unstructured"
                sling:resourceType="granite/ui/components/foundation/form/select"
                name="./missions">
                <datasource
                    jcr:primaryType="nt:unstructured"
                    sling:resourceType="/apps/mportal/datasources/missions"/>
            </field>
        </missions>
        <mtcs
            jcr:primaryType="nt:unstructured"
            sling:resourceType="granite/ui/components/foundation/form/multifield"
            fieldDescription="A user must have at least one of these mtcs to view this component"
            fieldLabel="MTCs">
            <field
                jcr:primaryType="nt:unstructured"
                sling:resourceType="granite/ui/components/foundation/form/select"
                name="./mtcs">
                <datasource
                    jcr:primaryType="nt:unstructured"
                    sling:resourceType="/apps/mportal/datasources/mtcs"/>
            </field>
        </mtcs>
        <languages
            jcr:primaryType="nt:unstructured"
            sling:resourceType="granite/ui/components/foundation/form/multifield"
            fieldDescription="A user must have at least one of these languages to view this component"
            fieldLabel="Languages">
            <field
                jcr:primaryType="nt:unstructured"
                sling:resourceType="granite/ui/components/foundation/form/select"
                name="./languages">
                <datasource
                    jcr:primaryType="nt:unstructured"
                    sling:resourceType="/apps/mportal/datasources/languages"/>
            </field>
        </languages>
        <startdate
            jcr:primaryType="nt:unstructured"
            sling:resourceType="granite/ui/components/foundation/form/datepicker"
            fieldLabel="Start Date"
            name="./startdate"/>
        <enddate
            jcr:primaryType="nt:unstructured"
            sling:resourceType="granite/ui/components/foundation/form/datepicker"
            fieldLabel="End Date"
            name="./enddate"/>
    </items>
</jcr:root>
1

1 Answers

1
votes

It turns out there is an issue with the way AEM Touch UI multifield components update values. If you remove the last item, the component sends no value up to the server (much like an unchecked checkbox). You have to explicitly tell AEM that you want to delete the value of a multifield if there are no items in it. Here's how:

  1. At the same level as your multifield node, create a new node (nt:unstructured); I call it permissions-delete (to correspond with my multifield called permissions)
  2. Add the following properties:
    1. sling:resourceType, String, granite/ui/components/foundation/form/hidden
    2. name, String, ./permissions@Delete (where ./permissions is the value of name field of the permissions/field node)
    3. value, Boolean, true

Now when you remove the last item, the value of the hidden field will ensure that it is deleted and not just ignored.