getMethodTriggerElementName() returns sometimes null

Gents

I’m trying to create a global method to validate input.
The idea is: “One table, One validate-method” for general record level validation. In this method multiple fields to check. (No duplicate code in forms, not tons of global methods for fieldvalidation)

In this method I use the function: getMethodTriggerElementName()
If this method is triggered in the form during onDataChange this funtions returns the current field corectly. When I attach this method to my validator in the dataprovider section it returns null . Surprise!

Why?
I want to obtain the current fieldname the validator is acting on.

example code:

// global method: val_rel_relation
//
var _form = currentcontroller.getName();
var _table = currentcontroller.getTableName();
var _field = application.getMethodTriggerElementName()
application.output(_form);
application.output(_table);
application.output(_field);
return true;

Servoy 3.5.2

Btw, I’m pretty new to Servoy so maybe I miss something trivial. :slight_smile:

Have you given the element/object in question a name? i.e. - typed something into the name property field at form/element design time?

Michael

Additionally I’d run the validation on data level. Not on the userinterface elements. If you disable autosave you can get an array of all changes and do your validations based on the actual changed data. Not very hard to implement.

You should avoid currentcontroller calls, because this is dangerous once you show several forms using tab panels of split beans. There is also a application.getMethodTriggerFormName() that should be used.

There is two things that you have to have in mind:

  1. (already mentioned): the element has to have a name (the name property cannot be null) that should be unique on that form
  2. You should not nest methods, because then the getMethodTriggerXXName() methods will return something wrong

Otherwise, this should work.

Patrick

Thanks for the quick response

  • yes, the element has a name.
  • Point taken about the use of currentcontroller.
  • Validation at datalevel and using array’s is interesting (if not promising). I was hoping however that the data-provider-validation is done AFTHER the form (ui) validation and BEFORE the pre-insert/update… methods.

Ok, enough info for me to go on. Still don’t understand why the same method in the same form on the same field triggered by different events provides a different result.

If you click on move sample on getEditRecords (under databasemanager) it gives you this:

//Get the edited records outstanding for a save
//This method can be used to loop through all outstanding changes,
//the application.output line contains all the changed data, their tablename and primary key
var editr = databaseManager.getEditedRecords()
for (x=0;x<editr.length;x++)
{
	var ds = databaseManager.getChangedRecordData(editr[x])
	var jstable = databaseManager.getTable(editr[x]);
	var tableSQLName = jstable.getSQLName();
	var pkrec = jstable.getRowIdentifierColumnNames().join(',');
	var pkvals = new Array();
	for (var j = 0; j < jstable.getRowIdentifierColumnNames().length; j++)
	{
		pkvals[j] = editr[x][jstable.getRowIdentifierColumnNames()[j]];
	}
	application.output('Table: '+tableSQLName +', PKs: '+ pkvals.join(',') +' ('+pkrec +')');
	// Get a dataset with outstanding changes on a record
	for( var i = 1 ; i <= ds.getMaxRowIndex() ; i++ )
	{
		application.output('Column: '+ ds.getValue(i,1) +', oldValue: '+ ds.getValue(i,2) +', newValue: '+ ds.getValue(i,3));
	}
}
//in most cases you will want to set autoSave back on now
databaseManager.setAutoSave(true);

That should cover what you want and it is triggered before the actual save to the database so you can cancel it.

Ok
There’s work to do.
Thanks

Unfortunately my weekend just started
:slight_smile: