xformsxsltforms

Dynamically (Js) replacing and validating an XSLTForms instance


I'm trying to manipulate an instance through Js code with no success at all, because everytime you reload the form, the instance is not working anymore if it contains 'binding' attributes in at leas one node. I know XSLT forms engine take some new values everytime it is loaded, and my -locally saved- instance files must be "revalidated", but I really can't find the way to do it.

I did some tryes, and they worked, but they do it until I close the forms. Everytime I re-open the form (and a new instance of XSLTForms engine is loaded) they can't be loaded anymore.

var model = document.getElementById('my-model').xfElement;

window.XsltForms_globals.openAction("XsltForms_change");        
xmlToLoadIn.parentNode.replaceChild(newXmlNode,xmlToLoadIn);
model.xfElement.addChange(newXmlNode);
window.XsltForms_globals.closeAction("XsltForms_change");

Also by injecting the xml object:

model.defaultInstance.src = "";
model.defaultInstance.srcXML = newXmlNode;

Or even trying everything I could see as a possible magic method:

model.rebuild();
model.recalculate();
model.revalidate();

Also with the instance:

model.defaultInstance.revalidate();
model.defaultInstance.validate_(newXmlNode); 
model.defaultInstance.validation_(newXmlNode);

And the only thing that worked without breaking the app was:

model.defaultInstance.setDoc(newXmlNode);

But it is not really working, because the instance is never loaded and when I navigate to the next subform, a message is shown: Unable to parse the XML.

In all the cases, except the last one that just erases the instance, the instance attributes are always the same, so I think these values are not being revalidated. E.g. xsltforms_depfor=" 44 48 52 58 8 12 18 4 ". So, I decided to remove all the attributes starting with xsltforms_ of the XML before saving it, and try to load it again, but it didn't worked either.

In all the cases, except the last one, the following exception is thrown: Error dispatching event 'xsltforms-revalidate': ... TypeError document.getElementById(...) is null

So, any idea how to dynamically load and revalidate a new instance from Javascript? Any idea is welcome, thanks in advance!


Solution

  • There were no problem at all with manipulating the instance structure through javascript. The problem was due to binding declarations that I used in subform to block the user to modify manually the data. E.g.

    <xf:bind nodeset="/application/params/param[@id='id']" readonly="true()" />
    <xf:bind nodeset="/application/params/param[@id='description']" readonly="true()" />
    

    I removed that and everything went ok, so I decided to make those input readonly through Js... As simple as that.

    The thing is the binding elements are assigned to the current subform, and part of the attributes depend on that number. When you load a different instance, the xsltform engine restart that "subforms count" and start again, and that seems to confuse the engine and throws the error because can't bind the binding elements.