Alexey Lugovoy
Alexey Lugovoy

Reputation: 101

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

Here's the problem, I need to validate the form before submitting in the next way, before user can submit anything he should click "Save" button, if he tries to click "Submit" one receives message something like "You should save form before submit".

First I thought that I can add system field to the form like save-indicator, add constraint to like that

<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 "Save" button beeing clicked.

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

Is there any posibility to make global variable like "isSaved" and check it for every form, before submit, and show error message if user didn't save form?

Or may be there another way that I can't see?


Will be appreciated for any answers.

Upvotes: 3

Views: 1314

Answers (2)

Jayy
Jayy

Reputation: 2436

I use a global flag indicator to check if the form is saved before closing the window or submit and it works pretty well.

This information is cleary explained in this wiki.

All the best!

Upvotes: 0

avernet
avernet

Reputation: 31753

Form Runner keeps track of whether the form is clean or dirty, and you can access that information in xxforms:instance('fr-persistence-instance')/data-status. The code handling the submit is in apps/fr/includes/persistence/persistence-model.xml. There you could change the listener for DOMActivate on fr-submit-button to read like:

<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:dispatch>
    </xforms:action>
    <xforms:action if="instance('fr-persistence-instance')/data-status = 'dirty'">
        <xforms:message>You must save form before submitting it.</xforms:message>
    </xforms:action>
</xforms:action>

Note that persistence-model.xml is in orbeon-form-runner.jar. To change that file, extract it from there, and place it in the WEB-INF/resources/apps/fr/includes/persistence/persistence-model.xml. That version on WEB-INF/resources will take precedence over the one in the jar file. Also note that these type of changes that rely on the internals of Form Runner or Form Builder have a chance to break when upgrading to a new version of Orbeon Forms. So you might want to carefully keep track of them, so you can more easily reapply the changes when you upgrade.

Upvotes: 2

Related Questions