Expand my Community achievements bar.

Presence of child objects different to that of parent subform creating a validation error(ES2)?

Avatar

Level 1

Hi All,

I've been able to change the mandatory state of an object based on its visibility like:

//javascript

//currentObject is passed as a var to this code in a function

if (currentObject.presence == "visible")// if the object is visible then make it mandatory

     {

     currentObject.validate.nullTest = "error"; //make mandarory

     }

     else //else if it's not visible it must not be mandatory

     {

     currentObject.mandatory = "disabled"; //make it non-mandatory

     }

This is okay if this is applied directly to the object as it's reading just the specific objects presence.

The problem I'm experiencing is if there are multiple fields in a subform and using an action I'm hiding the subform based on a condition (also working fine),

but when I go to validate the form on submit or with execValidate(), the presence state of the of the subform (hidden) is not cascading to the child fields in the subform.

That is, the subform and all fields contained herein are hidden on screen (works fine), but when I go to submit the form it is failing validation because these fields inside the subform are somehow still maintaing a presence of visible.

I've tested this with a dialogue to show me the value of the presence of the fields contained in the hidden subform and they are still showing "visible".

I'm guessing logic wise I would have to read the parent object from the current object to check if the parent is visible or not.  The issue will arise with a subform, in a subform containing the fields.  This indicates I'd have to read the parents visibility up the branch recurrsively but I don't know how.

Any suggestions or solutions would be greatly appreciated.

Thank you.

Best Regards

Chris 

1 Reply

Avatar

Level 10

One way is to use instances instead of hiding - if you set the subform instances to zero basically everything inside the subform ceases to exist so mandatory problems will go away. This will reset any data in the subform as well so isn't good if you want to retain data in the subform.

Set the subform as repeatable with a Min Count of zero and then you can use setInstances() to control the visibility. You need to use the underscore prefix for the Instance Manager so you can access items that don't exist yet (if it's instances are set to zero):

_Subform1.setInstances(1); //show subform

_Subform1.setInstances(0); //hide subform

Another way is to use a script to loop recursively through subforms to change the mandatory settings before or after hiding. The following are a couple of scripts I've used in Script Objects and call when needed:

//set fields mandatory

function mandatoryYes(vNode) {

    if (vNode.className === "field") {

        vNode.mandatory = "error";

    }

    for (var i = 0; i < vNode.nodes.length; i += 1) {

        mandatoryYes(vNode.nodes.item(i));

    }

}

//pass subform to operate on, xfa.form will do the whole form

mandatoryYes(xfa.form);

//set fields not mandatory

function mandatoryNo(vNode) {

    if (vNode.className === "field") {

        vNode.mandatory = "disabled";

    }

    for (var i = 0; i < vNode.nodes.length; i += 1) {

        mandatoryNo(vNode.nodes.item(i));

    }

}

//pass subform to operate on

mandatoryNo(xfa.form);