List Constraint for a Repeating Metadata Field and Commas
This blog is a reflection on Seed IMs experience with Alfresco when configuring a repeating (multiple) metadata field with a list of values that contain commas.
In general it is possible to use Content Model Constraints to handle lists of acceptable values for a metadata field. However, the issue we had to address was how to use constraints when commas were present in the value, (eg 2,4-d) and the metadata field is repeating. Alfresco uses commas to signify a new value when a user enters fields using Share. Therefore if you provide a dropdown which contains, for example, a value of 2,4-D, when you save the value, alfresco assumes that you are setting two values, 2 and 4-D into the multivalued metadata field. Unfortunately, the values are not valid from a constraint perspective so you cannot save them to the content item.
Solution
We tried many different options to get around this, such as escaping the comma or using share forms config “options” parameter but none of them worked, alfresco always treated 2,4-D as two values and set the metadata field accordingly. Eventually we settled on the solution to use a constraint value without a comma and use Resource bundles to present a value including a comma to the user. Therefore, the user picks a value from the list as 2,4-D but the real value saved is 2-4-D. The solution is shown below.
1) List Constraint: Create a list constraint in your custom content model. Note that comma in 2,4-D has been replaced with – (2-4-D). 2-4-D IBE Imazapic 2-4-D …
<constraint name=”seed:activeList” type=”LIST”>
<parameter name=”allowedValues”>
<list>
<value>2-4-D IBE</value>
<value>Imazapic</value>
<value>2-4-D</value>
…
</list>
</parameter>
</constraint>
2) Configure Metadata field to use List constraint values
<!– Muti-value Metadata field that uses the constraint –>
<property name=”seed:active”>
<title>Active</title>
<type>d:text</type>
<mandatory>true</mandatory>
<multiple>true</multiple>
<index enabled=”true”>
<atomic>true</atomic> <!– index in the foreground –>
<stored>false</stored> <!– store the property value in the index –>
<tokenised>true</tokenised>
</index>
<constraints>
<constraint ref=”seed:activeList”/>
</constraints>
</property>
3) Configure a Model Resource Bundle. This maps the values stored in the constraint to the Display values seen in Share for the dropdown. The space in 2-4-D IBE is escaped with value \u0020IBE as you cannot have spaces in a property name.
listconstraint.seed_activeList.2-4-D\u0020IBE=2,4-D IBE
listconstraint.seed_activeList.Imazapic=Imazapic
listconstraint.seed_activeList.2-4-D=2,4-D
…
4) Configure share form to allow for selectmany control to pick up constraint values.
<field id=”seed:active” read-only=”false” mandatory=”true” label-id=”label.nuf_active” set=”study-technical-properties” >
<control template=”/org/alfresco/components/form/controls/selectmany.ftl”> </control>
</field>
Reference
https://wiki.alfresco.com/wiki/Data_Dictionary_Guide#Model_Localization