Xforms: manage the masking of fields and the recording of the associated XML

How to keep the XML of hidden fields at registration, while emptying this XML of any values entered by the user before hiding.

Issue

For the usual use cases, the following behaviour is desired:

  • Hiding form fields by testing the value of other fields,
  • when the document is re-edited in the future, the form must allow the user to enter fields that were hidden during a previous registration,
  • on the other hand, if the user has entered values in a field which subsequently becomes hidden, we want the recorded XML not to contain these values (for the user the logic is that an invisible field has no values or texts entered).

But the default behaviour of Xform does not achieve this goal for 2 reasons:

  • By default, an irrelevant element is not serialized on registration (and therefore on re-editing, the associated field can no longer appear as the linked XML element is no longer present),
  • when this behaviour is modified (we will see how), any values entered before the field was hidden are serialized.

Our technical objective is to serialise only the structure of the XML, without values.

Step 1: How not to delete the irrelevant XML?

The simplest step, to tell Xform to serialise irrelevant XML elements, is to add relevant="false " to the submission element, for example :

<xf:instance id= »data »>
    <data>
        <value1 choice= »true »/>
        <value2 text= » »/>
    </data>
</xf:instance>

<xf:bind nodeset= »instance(‘data’)//instance/value2″ relevant= »../value1/@choice = ‘true' »/>

<xf:submission id= »save » ref= »instance(‘data’) » resource= »https://… » method= »post » replace= »none » relevant= »false »>
    <xf:action ev:event= »xforms-submit »/>
</xf:submission>

The XML created will contain value2 regardless of the value of value1/@choice.

Step 2: How do I empty the XML on registration?

For the moment, all our XML is taken into account at the time of saving, which means that when the document is re-edited, fields that were hidden during the previous saving can be displayed.
On the other hand, the irrelevant XML is not 'emptied', specifically as it stands:

  • If the user enters text in the field associated with value2/@text,
  • then hides this field by setting the control associated with value1/@choice to false,
  • then the XML saved to the submit will contain the text entered by the user in value2/@text, which is not what we want.

To solve this problem, one must use the Xform extensions (exf - https://www.exforms.org/) which are supported by Orbéon.

These extensions allow us to test the relevant status of an XML element, so we can write in our submit :

<xf:submission id= »save » ref= »instance(‘data’) » resource= »https://… » method= »post » replace= »none » relevant= »false »>
    <xf:action ev:event= »xforms-submit »>
        <xf:setvalue xxf:iterate= »//@text[not(exf:relevant(.))] » ref= ». »/>
    </xf:action>
</xf:submission>

The setvalue empties all text attributes that are not relevant (i.e. that are hidden due to user action), and several setvalues can be linked together to deal with the different cases!

Here we are, with these two steps, we arrive at a form that allows you to come back after saving the masked fields and to empty any values entered before masking!

Are you interested in this topic?

CONTACT US