I am trying to store a JSForm (created with solutionmodel) in a database, I want users to be able to go into designmode and edit their form and save this.
saved_form = plugins.serialize.toJSON(myForm);
("myForm " is a form variable, type media, “saved_form” is a textfield in de database)
As soon as the above code is executed it stops there and does nothing, no errors, nothing.
Is there any way to store the forms I created with solutionmodel in a database or can a JSForm not be serialized?
Not all objects in Servoy are capable of being serialized. What you could do is to build your own Objects/Array, etc that was used to build the JSForm, and then serialize that. Then have functions to read through the object and recreate the JSForm.
(have not tried this) Since JSON doesn’t work, perhaps store the JSForm object as a blob (‘media’ type) in the database?
saving a form in a blob using stringserializer also would use the json converter under the hood.
You cannot serialize a single form because a form is not self-contained, it exists within a hierarchy.
There is also no way to add a deserialized form to the solution model.
Rob
Hi Grimbert,
I am doing something similar at the moment. It is not possible to just save the form into the database, but you can read all the components of the form and then read all the properties of all the components in a loop and store that into a json object into the database.
When you want to rebuild the form you can get all your properties from your json object again and create the form.
Here is some sample code, note that this is only for fields:
To save:
var _fields = JSForm.getFields()
var _field, _fieldObject,
globals.fd_form = _form
for (var i = 0; i < _fields.length; i++) {
_field = _fields[i]
_to_form_builder$form.newRecord()
_to_form_builder$form.elementtype = 'FIELD'
_fieldObject = new Object()
for(a in _field)
{
if( typeof _field[a] != 'function')// don't want to save functions, just properties
{
if(a == 'valuelist' && _field[a])//valuelist have a object, we need just the name
{
_fieldObject[a] = _field[a].name
}
else if((/^on/.test(a) || /Media/.test(a)) && _field[a])//images have a object, we need just the name
{
_fieldObject[a] = _field[a].getName()
}
else
{
_fieldObject[a] = _field[a]
}
}
}
_to_form_builder$form.elementobject = _fieldObject
if(_fieldObject.name)
{
_to_form_builder$form.elementname = _fieldObject.name
}
else
{
_elem = 'elem_' + _to_form_builder$form.form_builder_id
_to_form_builder$form.elementname = _elem
_field.name = _elem
_fieldObject.name =_elem
}
databaseManager.saveData()
}
And to restore:
for (var i = 1; i <= _to_form_builder$form.getSize(); i++) {
_rec = _to_form_builder$form.getRecord(i)
if(!_rec.elementobject)
{
continue
}
if(_rec.elementtype == 'FIELD')
{
_component = _jsForm.newField(_rec.elementobject.dataProviderID, _rec.elementobject.displayType,_rec.elementobject.x, _rec.elementobject.y,_rec.elementobject.width,_rec.elementobject.height)
}
for (j in _rec.elementobject) {
if(j == 'valuelist')
{
_component[j] = solutionModel.getValueList(_rec.elementobject[j])
}
else if(/^on/.test(j))
{
_component[j] = _jsForm.getFormMethod(_rec.elementobject[j])
}
else if(/Media/.test(j) && _rec.elementobject[j])
{
_component[j] = solutionModel.getMedia(_rec.elementobject[j])
}
else
{
_component[j] = _rec.elementobject[j]
}
}
}
sanneke:
Hi Grimbert,
…
Thanks, it becomes a lot more clear now ![Smile :)]()