Skip to content

Lesson 7. Writing a PreSave ruleset

The PreSave ruleset is run in response to the user clicking the Save button in Formbird, and runs after the document's mandatory fields are verified, and before the document is actually saved.

There is an opportunity here to stop the Save, should the state of the document justify it, so it is an ideal place to script presave validation.

Step 1. Create the PreSave Ruleset

  • Open the Ruleset Editor for a new Ruleset

https://your-formbird/form/224bc62858d73ce57a9cb85e

  • Set the name of the RuleSet : Acme Rental Car - PreSave

  • Group can be set to acme, or your organisation name

  • Save your new ruleset.

The script pane will show the following:

{
#include "JayRule Ruleset Overlay JS",

    ruleset : {
        name : 'Acme Rental Car - PreSave',

        ruleInitialise : {
            ruleCondition : function(ntf) {
                return (ntf.runCount === 1);
            },

            ruleAction : function(ntf) {
            }
        }
    }
}
  • Link this ruleset to the "Acme Rental Car" template, event "PreSave", using the techniques described in Lesson 1.

Step 2. Add a validation rule for no future dates

Here we are going to validate that the Decommissioned date is not in the future. Only current or previous date-times are to be allowed. If the value violates this, then stop the Save.

  • Open the new ruleset Acme Rental Car - PreSave
  • Add a new rule called "ruleValidateDecommissionedDate"
  • Code the following for the ruleCondition function
ruleCondition : function(ntf) {
    var decomDate = ntf.document.decommissionedDate;
    var decomDateM = ft3.moment(decomDate);

    if (
        decomDate
        && decomDateM.isAfter()
    ) {
        return true;
    }
},

We make use here of the 3rd party date-time library "moment", which we call through ft3.

See Rulesets\moment (object) for more details.

Briefly, the function .isAfter() with no arguments returns true if the date is later than the current date-time.

  • Code the following for the ruleAction
ruleAction : function(ntf) { 
    ntf.errorMessage = 'The Decommissioned date must not be a future date.';
}

By setting ntf.errorMessage, we are telling the system that we have found a problem, and the save should not go ahead. Normally, ntf.errorMessage is null or undefined.

  • Save your ruleset.

Step 3. Add a validation rule for not earlier than the Opened Date

Here we validate that the Decommissioned date is not earlier than the Opened date.

  • Add a new rule called "ruleValidateDecommissionedDate2"
  • Code the following for the ruleCondition function
ruleCondition : function(ntf) {
    var decomDate = ntf.document.decommissionedDate;
    var decomDateM = ft3.moment(decomDate);
    var openedDateM = ft3.moment(ntf.document.openedDate);

    if (
        decomDate
        && decomDateM.isBefore(openedDateM)
    ) {
        return true;
    }
},
  • Code the following for the ruleAction
ruleAction : function(ntf) { 
    ntf.errorMessage = 'The Decommissioned date must not be before the Opened date.';
}
  • Save your ruleset.

Step 4. Testing

  • Open an existing Rental Car document.

  • Set the Decommissioned Date to three months in the future.

  • Save

  • The saving will be stopped, with an error message that you coded.

  • Set the Decommissioned Date to three months earlier than the Opened Date.

  • Save.

  • The saving will be stopped, with an error message that you coded.

  • Set the Decommissioned Date to a valid date, later than the Opened Date, and earlier or on the current date time.

  • Save.

  • The saving will proceed okay.

If the above are true, you have successfully implemented a PreSave ruleset!

Lesson Items covered

  • Creating and linking a PreSave ruleset
  • Use of the moment library object (introductory)
  • Setting of an error state in PreSave, to abort a document saving.