← servoy magazine

[Tips] Clearing form variables

  • Tips

[image unavailable: Picture 30]Form variables are a good way to show data from multiple sources in modal popup forms. When the modal popup is shown, fill in the data from the various sources, setup custom value lists, and you have a fast way of re-purposing data to a new destination — without cluttering up your global variables list with single purpose variables.

This can add up to a lot of form variables that you will need to clear out the next time you show the modal form. Instead of clearing out each form variable individually, place the following code at the top of the form's onShow event:

// clear all form variables
for (var i = 0; i < allvariables.length; i++) {
	forms.form_name_here[allvariables[i]] = null
}

This code snippet keeps doing its job no matter how many form variables you create, delete or modify.

Comments (3)

Ryan Parrish
I had a similar method a while back, but ran into the issue of not all my form vars should be reinitialized to the same value, 'null' in your example. So I wrote this little snip that resets the var's to their configured *default* values. function resetFormVars() { var myForm = solutionModel.getForm(currentcontroller.getName()); var frm_vars = myForm.getFormVariables(); for (var i =0; i<= frm_vars.length; i++) { var frm_var = frm_vars[i]; application.output(frm_var); currentcontroller.setDataProviderValue(frm_var.name,frm_var.defaultValue); } }
David Workman
Nice one Ryan. Another to add to the list: convert form variables into an object that can be passed around: // put form variables in obj var obj = {} for (var i = 0; i < allvariables.length; i++) { obj[allvariables[i]] = forms.ARCH_P_email__project_lead_initiation[allvariables[i]] } // send email forms.CAMP_0F_campaign.ACTION_web_form_email(obj, "ARCH_P_email__project_lead_initiation")
Servoy Stuff
Yes and for your email, once you passed your object, you can use VelocityReport.evaluateReport() with a template and $obj.properties in it ;)