resetting form variables

Questions, tips and tricks and techniques for scripting in Servoy

resetting form variables

Postby dpearce » Fri Feb 12, 2010 2:07 pm

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
dpearce
 
Posts: 469
Joined: Sun Dec 03, 2006 11:53 am

Re: resetting form variables

Postby ROCLASI » Fri Feb 12, 2010 2:23 pm

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).
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: resetting form variables

Postby dpearce » Fri Feb 12, 2010 2:48 pm

Thanks

David
dpearce
 
Posts: 469
Joined: Sun Dec 03, 2006 11:53 am

Re: resetting form variables

Postby pbakker » Fri Feb 12, 2010 3:18 pm

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

Paul
pbakker
 
Posts: 2822
Joined: Wed Oct 01, 2003 8:12 pm
Location: Amsterdam, the Netherlands

Re: resetting form variables

Postby david » Fri Feb 12, 2010 8:00 pm

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


Nice tip.
David Workman, Kabootit

Image
Everything you need to build great apps with Servoy
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Re: resetting form variables

Postby log-out » Wed Apr 13, 2011 12:35 pm

But I found a strange behaviour...

I have this global method:

Code: Select all
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:

Code: Select all
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:

Code: Select all
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
Log-Out
Madrid - Spain

Servoy 5.2.9 - build 1020 - SQL Server 2005
Servoy 6.0.3 - build 1223
Java version 1.6.0_29-b11 (!!)
Windows XP Professional SP3
log-out
 
Posts: 180
Joined: Sat Feb 19, 2005 4:20 pm
Location: Spain

Re: resetting form variables

Postby jcompagner » Wed Apr 13, 2011 3:06 pm

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.
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8829
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: resetting form variables

Postby log-out » Wed Apr 13, 2011 4:06 pm

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

Thanks
Log-Out
Madrid - Spain

Servoy 5.2.9 - build 1020 - SQL Server 2005
Servoy 6.0.3 - build 1223
Java version 1.6.0_29-b11 (!!)
Windows XP Professional SP3
log-out
 
Posts: 180
Joined: Sat Feb 19, 2005 4:20 pm
Location: Spain

Re: resetting form variables

Postby jcompagner » Wed Apr 13, 2011 4:15 pm

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)
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8829
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet


Return to Methods

Who is online

Users browsing this forum: Google [Bot] and 8 guests