Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Remove a subform permanently using javascript

Avatar

Level 5

I have a travel approval form that has two sections -- Domestic and Foreign.  Both sections have many required fields.  I set up a radio button at the top to select one of those options, so if the user selects Domestic, I want the Foreign section to disappear completely.  The problem I'm having is that it doesn't disappear -- it's no longer visible but it seems to reside somewhere in memory.  Then, when I use a Submit button to send the completed form, I get an error message saying there are required fields somewhere that are incomplete.  I have checked this scenario thoroughly, and the Domestic section is completely filled out.  I have made all fields visible to make sure I'm not missing anything.  When I remove the Foreign section within LiveCycle, the problem goes away, so I'm pretty sure that it's giving me the error because it detects the required fields in the Foreign section as incomplete.

In the radio button at the beginning of the form, I have:

// Show "DOMESTIC" section

if (this.rawValue == "1") {

  form1.Domestic.presence = "visible";

  form1.Foreign.presence = "inactive";

}

// Show "FOREIGN" section

if (this.rawValue == "2") {

  form1.Foreign.presence = "visible";

  form1.Domestic.presence = "inactive";

}

For lines 4 and 10 I have also tried using:

_Foreign.setInstances(0);

In the Submit button of the Domestic section I have this code:

if (_Foreign.count > "0") {

  _Foreign.occur.min = "0";

  Foreign.presence = "inactive";

}

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hello -

The reason you are getting the error is because the system is also validating the "hidden" group (Foreign) even though it's hidden. Same thing would happen if Foreign is visible and all fields are completed, but the Domestic group is hidden. It will still attempt to validate the Domestic fields unless you wrote a script to "exclude" them.

One way to resolve the issue is by setting up the groups as "instances" and then add & remove the instances via the radio button. So initially, both instances will be set to 0 when the radio button is unchecked, then choosing one or the other radio button will set the applicable instance to 1 and the other to 0. For example, checking Domestic will set it's instance to 1 and set the Foreign instance group to 0, effectively removing it from the page. By doing this, you are removing the instance from the form completely (vs changing its presence to hidden/inactive) thus it wouldn't be validated.

Try the script below. NOTE: Please ensure all of the Domestic and Foreign content is placed in separate subform groups (if you haven't done so already), then ensure "Repeat Subform for Each Data Item" is checked for each subform group under the Binding tab. Also, ensure the Max box is checked and set to "1" for each group since you aren't trying to "add" multiple instances, but simply controlling the single existence of each instance. Also, ensure the export values of the radio buttons is set to 1 for Domestic, and 2 for Foreign.

***********************************

Place script on Click event of the main radio button group (NOT the individual radio button)

// Set the instances of each of the subforms

// Note "_" is shorthand for instanceManager

if (this.rawValue == 1) {//Domestic radio button is checked

_Domestic.setInstances(1);//note: ensure correct name and path of Subform group

_Foreign.setInstances(0);//note: ensure correct name and path of Subform group

}

else if (this.rawValue == 2) {//Foreign radio button is checked

_Domestic.setInstances(0);

_Foreign.setInstances(1);

}

else {//radio button field is reset...neither Domestic or Foreign is checked

_Domestic.setInstances(0);

_Foreign.setInstances(0);

}

View solution in original post

5 Replies

Avatar

Correct answer by
Level 6

Hello -

The reason you are getting the error is because the system is also validating the "hidden" group (Foreign) even though it's hidden. Same thing would happen if Foreign is visible and all fields are completed, but the Domestic group is hidden. It will still attempt to validate the Domestic fields unless you wrote a script to "exclude" them.

One way to resolve the issue is by setting up the groups as "instances" and then add & remove the instances via the radio button. So initially, both instances will be set to 0 when the radio button is unchecked, then choosing one or the other radio button will set the applicable instance to 1 and the other to 0. For example, checking Domestic will set it's instance to 1 and set the Foreign instance group to 0, effectively removing it from the page. By doing this, you are removing the instance from the form completely (vs changing its presence to hidden/inactive) thus it wouldn't be validated.

Try the script below. NOTE: Please ensure all of the Domestic and Foreign content is placed in separate subform groups (if you haven't done so already), then ensure "Repeat Subform for Each Data Item" is checked for each subform group under the Binding tab. Also, ensure the Max box is checked and set to "1" for each group since you aren't trying to "add" multiple instances, but simply controlling the single existence of each instance. Also, ensure the export values of the radio buttons is set to 1 for Domestic, and 2 for Foreign.

***********************************

Place script on Click event of the main radio button group (NOT the individual radio button)

// Set the instances of each of the subforms

// Note "_" is shorthand for instanceManager

if (this.rawValue == 1) {//Domestic radio button is checked

_Domestic.setInstances(1);//note: ensure correct name and path of Subform group

_Foreign.setInstances(0);//note: ensure correct name and path of Subform group

}

else if (this.rawValue == 2) {//Foreign radio button is checked

_Domestic.setInstances(0);

_Foreign.setInstances(1);

}

else {//radio button field is reset...neither Domestic or Foreign is checked

_Domestic.setInstances(0);

_Foreign.setInstances(0);

}

Avatar

Level 5

Sorry, I cried victory too soon.....  The form seems to "forget" that the instance was set to zero, and after I save it and reopen it, the previously deleted instance shows up with all blank fields. I've got the form saving to version 9.1 and later.  Any ideas?

Avatar

Level 6

Hi Geckoz100 -

It could be one of 2 things (that I can think of)....

Starting with the most likely...Under Form Properties > Run-time...ensure "Automatically" is selected (vs Manually), under "Preserve scripting changes to form when saved". This option ensure the form changes like hiding subforms remain intact (automatically) when the form is saved.

If that doesn't work, try naming the fields (contained in each instance group) with unique names . vs using the default names. Also under the binding tab, ensure "Use name ()" is selected under Data Binding (Open,save, Submit) vs "No data binding". This may not be the reason hence I say start with the first option above.

Avatar

Level 5

Thanks, I checked the "Preserve scripting changes" setting and it was already set.  I had only one field set to global binding, so I disabled that and it still had the same problem.  I do have a lot of the same field names in both sections, such as Departure Date, Return Date, etc..  It would be too much work to rename them all to unique names.  The solution I found was to put a script in the docReady event of the main selector button (Domestic/Foreign), to re-apply the setInstances count when re-opening the document later:

// DOMESTIC

if (this.rawValue == "1")   {

  form1.Foreign.occur.min = "0";

  form1._Foreign.setInstances(0);

}

// FOREIGN

if (this.rawValue == "2")   {

  form1.Domestic.occur.min = "0";

  form1._Domestic.setInstances(0);

}

It's not the cleanest setup, but at least it works.  I tested it pretty thoroughly and even after multiple openings/closings the file still retains the proper sections and the Submit buttons work without reporting "fields incomplete" error like before.

Thanks again for the responses.