== 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 in the nodeset and add [. > 2.0] to end of the item predicate.
== 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 .='true' 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.
Note that the functionally is similar to a and combination but no 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:
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'.
== 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.
== Discussion ==