3

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.

  • I think you are looking for state handling. But why do you need forms to be 'saved' before submit? The XForms model should be up to date already like I mentioned here as well: http://stackoverflow.com/questions/9737194/how-to-save-user-entered-values-in-xforms-textbox/9737969#9737969 – grtjn Mar 27 '12 at 15:57
  • It seems that you know how to do this for one form, but would like a way to pull that functionality into many forms without having to duplicate code; is that right? Also, are you using Form Builder, or writing the XForms "by hand"? – avernet Mar 27 '12 at 17:34
  • avernet, yes you're right I'm using the Form Builder, and just as you propose I don't want duplicate code. – Alexey Lugovoy Mar 28 '12 at 05:18
  • grtjn, after submitting the model is saved, but the trick is "save" button returns "form.html" to download, so it's like saving on the client PC, when user opens "form.html" he goes to the form that he was filling. If he don't save form and will submit it, he will never know UUID of the form, that he was sending. – Alexey Lugovoy Mar 28 '12 at 05:25
  • @AlexeyLugovoy Got it; so I posted an answer below. – avernet Mar 29 '12 at 01:21

2 Answers2

2

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.

avernet
  • 30,895
  • 44
  • 126
  • 163
  • Is it possible that we develop a xform which is built by form builder and then code it "by hand" ? – Jayy Mar 29 '12 at 13:53
  • 1
    @KaipaMSarma The easiest way to put your own custom code in a form created with Form Builder is to use the *Edit Source* functionality from the sidebar. If at a certain point you're not planning to do any more editing in Form Builder, you could also copy the source to disk, in the right folder under `WEB-INF/resources/forms`, and then do all the editing exclusively "by hand", i.e. with a text/XML editor. – avernet Mar 29 '12 at 17:50
0

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!

Jayy
  • 2,368
  • 4
  • 24
  • 35