How can I create my own validation rule in orbeon xforms?

Here is the problem, I need to validate the form before submitting as follows, before the user can submit anything he has to click on the "Save" button, if he tries to click on "Submit", then he will get a "Save Forms Before Submit" message.

At first I thought I could add a system field to the form like save-indicator, add a constraint like

<xforms:bind id="isSaved-bind" nodeset="isSaved" 
  name="isSaved" type="xforms:string" constraint="number(.)=1" required="true()"/>


And add

<xforms:setvalue ref="xxforms:instance('fr-form-instance')/person/isSaved">1</xforms:setvalue>  


to actions when clicking the "Save" button.

But the problem is that I have to rewrite all existing forms to insert new code there.

Is it possible to make a global variable like "isSaved" and check it for each form before submitting and show an error message if the user hasn't saved the form?

Or maybe there is another way that I can't see?

Any answers will be appreciated.


source to share

2 answers

The Form Runner keeps track of whether the form is clean or dirty and you can access this information in xxforms:instance('fr-persistence-instance')/data-status

. The code that handles the submission is in apps/fr/includes/persistence/persistence-model.xml

. There you can change the listener for DOMActivate

to fr-submit-button

to read:

<xforms:action ev:event="DOMActivate" ev:observer="fr-submit-button">
    <xforms:action if="instance('fr-persistence-instance')/data-status = 'clean'">
        <xforms:setvalue ref="instance('fr-persistence-instance')/submit-or-save-or-send">submit</xforms:setvalue>
        <xforms:dispatch name="fr-save-action" target="fr-persistence-model">
            <xxforms:context name="fr:check-data-valid" select="true()"/>
    <xforms:action if="instance('fr-persistence-instance')/data-status = 'dirty'">
        <xforms:message>You must save form before submitting it.</xforms:message>


Please note what persistence-model.xml

is in orbeon-form-runner.jar

. To modify this file, extract it from there and place it in WEB-INF/resources/apps/fr/includes/persistence/persistence-model.xml

. This version WEB-INF/resources

will not take precedence over the file in the jar file. Also note that these types of changes, which depend on the internal components of the Form Runner or Form Builder, have a chance of breaking when upgrading to a newer version of Orbeon Forms. This way you can track them closely so that you can more easily reapply the changes when you upgrade.



I am using the global flag indicator to check if the form persists before the window is closed or submitted and it works very well.

This information is explained in this wiki .

All the best!



All Articles