Add elements from code

Hello!

I want to add elements like labels, field and other from the code creating a new form.

I try this but it didn’t work:

function onAction(event) {
	var sForm = "testForm"
	history.removeForm(sForm);
	controller.recreateUI();
	
	// Get the form
	var password = "ciao"
	var myForm = solutionModel.newForm("asd",null,null,null,false,500,500)
	myForm.newLabel('Test', 20, 20, 120, 30)
	myForm.newLabel("ASDADASDASDASDASDASD",150,150,500,400).background='green'
	myForm.newPassword(password,200,100,150,50)
}

I create a new empty form called “testForm”, and when i click to the button i want to populate this form with my fields…how can i do this?

Thanks

So you want to change your existing form “testForm”, correct?
In that case you should use the function solutionModel.getForm() to get the formObject, then make your changes and in the end call controller.recreateUI():

function onAction(event) {
   var sForm = "testForm";
   
   // Get the form
   var password = "ciao";
   var myForm = solutionModel.getForm(sForm);
   myForm.newLabel('Test', 20, 20, 120, 30);
   myForm.newLabel("ASDADASDASDASDASDASD",150,150,500,400).background='green';
   myForm.newPassword(password,200,100,150,50);
   controller.recreateUI();
}

Thanks but it doesn’t work … :confused:

here is my solution can you test it please?

i can’t understand where the error is :confused:

creaFinestra.servoy (4.37 KB)

Whenever you change something with the solutionModel() you have to call recreateUI(), otherwise you get the error “Stale form(s) detected” when you are changing the form again.
In the onLoad method of your main-form, you don’t do that.

Also note that you don’t have to remove the form from history anymore before making changes with the solutionModel. Since the introduction of the recreateUI() function, that isn’t necessary anymore.

So if you change the onLoad method on your main form to this:

function onLoad(event) {
	var __sForm = forms.testForm.controller.getName();
	
	var _form = solutionModel.getForm(__sForm)
	
	//Create form
	_form.navigator = SM_DEFAULTS.NONE;
	_form.newLabel("Test label",10,10,100,30);
	var _labelVar = "text";
	_form.newField(_labelVar,JSField.TEXT_FIELD,50,50,100,50);
	forms[__sForm].controller.recreateUI();
	forms[__sForm].controller.show(_form);
}

and the _btnAddElementmethod on your testForm to this:

function _btnAddElement(event) {
 	var __sForm = event.getFormName();
 	application.output(__sForm);
	var _form = solutionModel.getForm(__sForm);
	
	_form.newLabel("New label insert with the button!",40,40,300,20);
	var something = "asd";
	_form.newField(something,JSField.TEXT_FIELD,60,60,200,30);
	controller.recreateUI();
}

Then it works correct.

Thanks a lot! :D