resetting form variables

I think this has been asked before, but any years ago!

Is there any of resetting all the form variables, rather than having to copy and paste them into a new method to set them back to the original definitions?

Is there a command for this yet?

forms.test.controller.resetformvariables() ??? would be nice

David

Hi David,

You can write a simple (global) method for this since a form has an allvariables property that returns an array with your form variables.
Just iterate this array and set all variables to null (or another value).

Thanks

David

And using the SolutionModel you can retrieve the variables defaultvalue, if it had one.

Paul

pbakker:
And using the SolutionModel you can retrieve the variables defaultvalue, if it had one.

Nice tip.

But I found a strange behaviour…

I have this global method:

function resetFormVars(formName)
{
	var myForm = solutionModel.getForm(formName);
	for (var i in forms[formName].allvariables) {
		var formV = myForm.getFormVariable(forms[formName].allvariables[i]);
		forms[formName].controller.setDataProviderValue(formV.name, formV.defaultValue);
	}
}

Where formName is the name of the Form where this method is called.

formV.defaultValue doesn’t work properly (I think) because when the default value of a variable is null, it returns the string “NULL” :shock: :shock:
Ok, I have to modify the method:

function resetFormVars(formName)
{
	var myForm = solutionModel.getForm(formName);
	for (var i in forms[formName].allvariables) {
		var formV = myForm.getFormVariable(forms[formName].allvariables[i]);
		if(formV.defaultValue.toUpperCase() == 'NULL') {
			forms[formName].controller.setDataProviderValue(formV.name, null);
		} else {
			forms[formName].controller.setDataProviderValue(formV.name, formV.defaultValue);
		}
	}
}

But… it’s no ok!! Again, with formV.defaultValue. When the default value is a string, formV.defaultValue return a string with single quotes: “‘default_value’”. So I have to modify again the method, like this:

function resetFormVars(formName)
{
	var myForm = solutionModel.getForm(formName);
	for (var i in forms[formName].allvariables) {
		var formV = myForm.getFormVariable(forms[formName].allvariables[i]);
		if(formV.defaultValue.toUpperCase() == 'NULL') {
			forms[formName].controller.setDataProviderValue(formV.name, null);
		} else {
			//SI LA VARIABLE ES STRING, SERVOY SE EMPEÑA EN PONER UNAS COMILLAS, ASI QUE TOCA QUITARLAS
			forms[formName].controller.setDataProviderValue(formV.name, utils.stringReplace(formV.defaultValue, "'", ""));
		}
	}
}

It’s very annoying

solution model JSVariable.defaultValue can’t be used like that.
Its a string representation of the the default value (so “serialized” to a string)

We have all kinds of logic to assign it eventually the right value, some values we even “eval” so that it becomes a real js object.

Ops… so How can I reset the form variables to their default value?

Thanks

you can use the default value but then you have to interpreted it just like we do

I think the easiest and quickest way would be to remember the variables somewhere as there real js objects.

Or else remove from history so that it will be destroyed and recreated (but that is quite big)