XForms: Using calculate and xxforms:default attribute in nodeset binding
-
30-09-2019 - |
Question
I have form1 passing a query string parameter(param1) to form2. I pass param1=true from form1. In form 2, I am trying to set the value of the param1 into a local instance. If I DO NOT pass param1 from form 1, I want form2 to take the value of param1 to be false. Here is my instance.
<xforms:instance id="querystring-instance">
<query-string>
<param1></param1>
</query-string>
</xforms:instance>
I am using the following line to set the value of param1 by fetching it from the query string. If query string does not have param1, I want to use the default value of false.
<xforms:bind nodeset="instance('querystring-instance')/param1" calculate="xxforms:get-request-parameter('param1')" xxforms:default="false()" />
This does not work. If param1 is present it works and sets the in querystring-instance to true. If param1 is not present, it sets to nothing. I want it to be set to false. How do I do this?
Solution
You probably want to set the parameter upon initialization only, so use xxforms:default
instead of calculate
. calculate
will be evaluated at each XForms recalculate, and that will fail because xxforms:get-request-parameter()
is only available during XForms initialization.
You can implement the condition in XPath.
xxforms:get-request-parameter()
returns and empty sequence if the parameter is missing. This should work:
<xforms:bind nodeset="instance('querystring-instance')/param1"
xxforms:default="(xxforms:get-request-parameter('param1'), 'false')[1]"/>
What this does is that if xxforms:get-request-parameter()
returns an empty sequence, then the first value of the sequence will be 'false', and that's what will be used to set the value.