XForms/Relevant

Using Bind with the Relevant Attribute
XForms also allows you to conditionally display part of a form based on some value in your instance data. Typically this is used when the answer to one field of the form conditionally displays another part.

The format of this is to use the xf:bind statement within in the xf:model.

The following examples demonstrate this.

Link to Relevancy XForms Application
Relevancy Demos Note: RelevancySelector.xhtml in the example application is incomplete.

Bind to a Decimal
In the following example, the second input field is conditionally displayed based on the integer value of the first instance value.

Using Multiple Predicates in Binds
Sometimes you have a sequence of many items and the display of one item depends on the values of other items. Predicates are a way of appending AND/OR operations to the end of a path expression used as a relevancy expression.

The following example uses a bind with two predicates. The second item in a sequence is bound to the first item. To do this you must select the second item item[2] in the nodeset and add '''[. &gt; 2.0] to end of the item[1]''' predicate.

    First item:  

 Second item:  

Bind to Boolean
Since parts of a form are usually either visible or not visible, it is natural to use a boolean value to determine if the field should be displayed.

In this example if InputIndicator is true, the second output instance is visible.

If the InputIndicator is false, the second output is not visible.

Note that expression  is used. This is a string comparison. Ideally you would just be able to test a node using mynoode=true but I have had problems with this method.

In this example, a simple checkbox is used to conditionally display a field.

In this example the type cast to boolean does not have any impact since relevant="/var/first=true" does not work as expected. String comparison must be used.

Binding a view to a select 1
This example shows how to conditionally display a view based on the value of a select1 control.      </xf:model> Demonstration of relevant fields. <xf:select1 ref="/data/select1value"> <xf:label>What view would you like to see? </xf:label>

<xf:item select="yes"> <xf:label>View 1</xf:label> <xf:value>1</xf:value> </xf:item> <xf:item> <xf:label>View 2</xf:label> <xf:value>2</xf:value> </xf:item> <xf:item> <xf:label>View 3</xf:label> <xf:value>3</xf:value> </xf:item> </xf:select1>

<xf:input ref="/data/view1"> <xf:label>First view: </xf:label> </xf:input> <xf:input ref="/data/view2"> <xf:label>Second view: </xf:label> </xf:input> <xf:input ref="/data/view3"> <xf:label>Third view: </xf:label> </xf:input>

Note that the functionally is similar to a <xf:switch> and <xf:case> combination but no <xf:toggle> is used.

The bind statement
The following line is the line that does the actual bind. You should use the instance function whenever you are binding one instance to another.

Also note that you must compare the text of the InputIndicator (the period) to true and not the InputIndicator itself.

An Architecture for Conditional Views and User-Maintainable Rules
Sometimes you need to have a consistent way of conditionally displaying views in a form. For example, if you want non-programmers to maintain the business logic of when a view is displayed, the binding rules to a view can be generated by an external rules engine.

To make this work you need to create a central instance that is used to control form views:

</xf:instance>

We call this architecture one of "Named Views" because an external tool can be used to state the rules of how any named view is rendered. This will be a central location in the form that generates the view instance and the binding expressions.

Each view is wrapped in a group element that binds the group to the instance in the view:

The display rules for each view can then be stored in a bind statement:

Each form can then have a "rules file" that looks similar to the following:

You can then build another XForms application that allows non-programmers to maintain these rules files and and a simple transform that places the instance and bind statements in the XForms model when it is loaded. The original groups may still need to be manually added to the form but once the views are added the rules can be maintained with a separate application.

Changing Fields to Display
The following example conditionally displays two zip code fields if the country code is 'usa" or displays a postal code if the field is not 'usa'.

</xf:instance> <xf:instance xmlns="" id="views"> </xf:instance> <xf:bind id="DisplayZipCode" nodeset="instance('views')/DisplayZipCode" relevant="instance('save-data')/CountryCode='usa'" /> <xf:bind id="DisplayPostalCode" nodeset="instance('views')/DisplayPostalCode" relevant="not(instance('save-data')/CountryCode='usa')" /> </xf:model> Demonstration of binding Zip Code Input <xf:select1 ref="instance('save-data')/CountryCode" selection="open"> <xf:label>Country:</xf:label> <xf:item> <xf:label>USA</xf:label> <xf:value>usa</xf:value> </xf:item> <xf:item> <xf:label>Canada</xf:label> <xf:value>can</xf:value> </xf:item> <xf:item> <xf:label>Mexico</xf:label> <xf:value>mex</xf:value> </xf:item> <xf:item> <xf:label>Other</xf:label> <xf:value>other</xf:value> </xf:item> </xf:select1> <xf:group bind="DisplayZipCode"> <xf:input ref="instance('save-data')/ZipCode" class="ZipCode"> <xf:label>Zip Code: </xf:label> </xf:input> <xf:input ref="instance('save-data')/ZipCodeSuffix" class="ZipCodeSuffix"> <xf:label>-</xf:label> </xf:input> </xf:group> <xf:input ref="instance('save-data')/LocationPostalID" bind="DisplayPostalCode" class="DisplayPostalCode"> <xf:label>Postal Code: </xf:label> </xf:input>

Delete
One of the most common occurrence of conditional display is the delete function for repeating elements. Typically you do not want to delete the last one. See the XForms/Conditional_delete example.