xf:readonly, xf:required, xf:relevant and xf:constraint CTFP's

Smartsite 7.2 - ...

The xf:readonly, xf:required, xf:relevant and xf:constraint CTFP's can be used to directly specify an XPath expression which sets the underlying model item property.

xf:readonly

When the xf:readonly ctfp has been set on a contenttype field, the XForms generator will add a bind element to the model logic, referencing this contenttype field, and set the readonly model item property of this element to the specified (XPath) expression.

The readonly property is used to disable controls. When the specified XPath expression evaluates to true, the node content is restricted from changing, thus the html control will be disabled.

Example

Suppose you have three contenttype fields: includepicture (a checkbox), picturecaption (a textbox) and picture (an iteminput). As long as the checkbox isn't selected, you want to disable the picturecaption and picture input controls.

To do so, you must add a xf:readonly ctfp to both of these fields with the value:

../cms:includepicture='false'

This will result in two bind elements after the XForms has been generated:

XML CopyCode image Copy Code
<xf:bind nodeset="cms:entry/cms:picture" readonly="../cms:includepicture='false'" /> <xf:bind nodeset="cms:entry/cms:picturecaption" readonly="../cms:includepicture='false'" />

xf:relevant

Similar to the xf:readonly ctfp, when the xf:relevant ctfp has been set, a bind element will be added to the model logic, and the relevant property will be set to the specified expression.

The relevant property is used to hide controls. When the specified XPath expression evaluates to true, the bound control(s) will be hidden.

Example

Assuming the same fields as the example above, but in this case you want to hide the picturecaption and picture input controls.
To do so, add the ctfp xf:relevant to both of these fields, with the value:

../cms:includepicture='true'

This will result in the following two bind elements after the XForms has been generated:

XML CopyCode image Copy Code
<xf:bind nodeset="cms:entry/cms:picture" relevant="../cms:includepicture='true'" /> <xf:bind nodeset="cms:entry/cms:picturecaption" relevant="../cms:includepicture='true'" />

xf:required

The xf:required ctfp can be used to set a field required conditionally, that is, using an XPath expression to indicate a field is required when another field has a specific value (for example).

Example

Again, the same scenario with the three picture fields. It makes sense to require input on the picture and picturecaption input controls when the includepicture checkbox has been checked.

To do so, add the ctfp xf:required to both of these fields, with the value:

../cms:includepicture='true'

This will result in the following two bind elements after the XForms has been generated:

XML CopyCode image Copy Code
<xf:bind nodeset="cms:entry/cms:picture" required="../cms:includepicture='true'" /> <xf:bind nodeset="cms:entry/cms:picturecaption" required="../cms:includepicture='true'" />

Another example, making a field required when activating an item, the following expression can be used:

instance('state')/nextworkflowstate = 'WF_COMPLETE_PUBLISH'

xf:constraint

With the xf:constraint ctfp, you can specify an XPath constraint rule for a specific field.

Examples

To restrict the number of characters that a text field may contain (to 150 in this case), you could use e.g. the expression:

string-length(.) &lt;=150

Which will result in the following bind element:

XML CopyCode image Copy Code
<xf:bind nodeset="cms:entry/cms:title" constraint="string-length(.) &lt;=150" />

Another example: two (required) datetime fields, e.g. "enddate" and "startdate" and you want to validate that the "enddate" is not before the "startdate".

Since datetime fields are written in a special xml format ("yyyy-mm-ddThh:mm:ss", e.g. "2021-11-19T14:34:17.777") within the instance, you can simply use a string compare to achieve this.
The expression, which must be set on the "enddate" field, would be: compare(string(.),string(../cms:startdate)) != -1

Which will result in the following bind element:

XML CopyCode image Copy Code
<xf:bind nodeset="cms:entry/cms:enddate" constraint="compare(string(.),string(../cms:startdate)) != -1" />