Html forms issue with nested obsgroups

Hello everyone,

we have an issue with multiple obs groups nested in multiple obsgroups nested in repeat tag.

We have something like that:

<repeat>
  <template>
  
  <div id="{n}-outerDiv">
      
	  <obsgroup id="{n}-outerObsGroup" groupingConceptId="XXX">
	
		<div id="{n}-sectionA">
			<obs conceptId="ABC" />
			<obs conceptId="DEF" />
		</div>
	
	
		<div id="{n}-sectionB">
		
			 <div id="innerDiv-1">
				<obsgroup id="innerObsGroup-1" groupingConceptId="YYY">
					<obs conceptId="GHI" />
					<obs conceptId="JKL" />
					<obs conceptId="MNO" />
				</obsgroup>
			</div>
			
			 <div id="innerDiv-2">
				<obsgroup id="innerObsGroup-2" groupingConceptId="YYY">
					<obs conceptId="GHI" />
					<obs conceptId="JKL" />
					<obs conceptId="MNO" />
				</obsgroup>
			</div>
			
			 <div id="innerDiv-3">
				<obsgroup id="innerObsGroup-3" groupingConceptId="YYY">
					<obs conceptId="GHI" />
					<obs conceptId="JKL" />
					<obs conceptId="MNO" />
				</obsgroup>
			</div>
			
		</div>
		
      </obsgroup>
  </div>
  
	</template>

	<render n="1" />
	<render n="2" />
	<render n="3" />
	<render n="4" />
	<render n="5" />
	<render n="6" />
	<render n="7" />
	<render n="8" />
	<render n="9" />
	<render n="10" />
	
</repeat>

It renders 10 sections/divs which each of them contains together 11 obses (2 from ‘sectionA’, and 3x3 from ‘sectionB’).Each ‘package’ of three obses is grouped into obsgroup with groupingConceptId=“YYY” and all these three ‘packages’ + two concepts from sectionA (obs conceptId=“ABC” and obs conceptId=“DEF”) should be grouped into groupingConceptId=“XXX”.

The issue is that sometimes when saving the form in EDIT mode, inner obsgroup (with groupingConceptId=“YYY”) is not assigned to its parent (obsgroup is NULL) therefore obs with conceptIds “GHI”, “JKL”, “MNO” are not displayed on the UI at all. If I assign directly in database proper obsgroup id then ‘packages’ of three obs are displayed correctly.

And another issue is when we fill in for example three templates:

n = 1 → we fill in ABC, DEF and GHI from innerObsGroup-1

n = 2 → we fill in ABC, DEF and GHI from innerObsGroup-1

n = 3 → we fill in ABC, DEF and GHI from innerObsGroup-1

then ABC and DEF obses are assigned and displayed properly (each to own template), but all three “GHI” obses are displayed in the first template and in the second and third template GHI obs is blank (because all is assigned to the first template).

Does anyone know how to solve it or what are we doing wrong? Is this case supported by OpenMRS at all? When we have only one outer obsroup and inside this are multiple obsgroups that probably works fine, but in this case we have multiple outer obsgroups and inside each obsgroup there is multiple obsgroups children.

Thank you in advance for any comments.

@druchniewicz this sounds like a complicated issue, and the logic for handling obs groups in HFE is a bit gnarly. It would be helpful to narrow down the issue a bit. For example, if you remove the repeat/template tags (and do it all the long way), does the issue persist? (i.e. can we eliminate the repeat/template tags as a possible source of the issue). Then, can you simplify the example down to the fewest number of obs/obsgroup combinations that allows you to reproduce the problem? That would make it easiest for someone to help take up the troubleshooting.

@mseaton thank you for response. Agree that logic in HFE is quite complex and difficult to understand. After further investigation I think there is something issue with EDIT mode. Assume I have simply form:

<htmlform formUuid="225654b8-7b9d-460a-bbdd-4929b673579b" formName="test123"
          formEncounterType="ee47cf72-e042-40f4-9765-5eec5194c7b4" formVersion="1.0">
	
<style>
	.breakLine {
		color:red;
	}
</style>

<h2>test123 (v1.0)</h2>

<section headerLabel="1. Encounter Details">
	<table class="baseline-aligned">
		<tr>
			<td>Date:</td>
			<td><encounterDate default="today"/></td>
		</tr>
		<tr>
			<td>Location:</td>
			<td><encounterLocation/></td>
		</tr>
		<tr>
			<td>Provider:</td>
			<td><encounterProvider/></td>
		</tr>
	</table>
</section>

<div>
	<div id="section1">	
		<obsgroup groupingConceptId="166429">
			<div id="personalData1">
				<span>Name</span><obs id="name" conceptId="166424"/>
				<span>Surname</span><obs id="surname" conceptId="166425"/>
			</div>
			
			<obsgroup groupingConceptId="166430">
				<br></br>
				<br></br>
				<div id="schoolAndWork1-1">
					<span>Start date</span><obs id="startDate" conceptId="166426" allowFutureDates="true"/>
				</div>
			</obsgroup>
			
			<obsgroup groupingConceptId="166456">
				<br></br>
				<div id="schoolAndWork1-2">
					<span>Start date</span><obs id="startDate" conceptId="166426" allowFutureDates="true"/>
				</div>
			</obsgroup>
			
			<obsgroup groupingConceptId="166457">
				<br></br>
				<div id="schoolAndWork1-3">
					<span>Start date</span><obs id="startDate" conceptId="166426" allowFutureDates="true"/>
				</div>
			</obsgroup>
		</obsgroup>
	</div>
		<br></br>
		<br></br>
		<span class="breakLine">******************************************************************************************</span>
	
	<div id="section2">
		<obsgroup groupingConceptId="166429">
			<div id="personalData2">
				<span>Name</span><obs id="name" conceptId="166424"/>
				<span>Surname</span><obs id="surname" conceptId="166425"/>
			</div>
			
			<obsgroup groupingConceptId="166430">
				<br></br>
				<br></br>
				<div id="schoolAndWork2-1">
					<span>Start date</span><obs id="startDate" conceptId="166426" allowFutureDates="true"/>
				</div>
			</obsgroup>
			
			<obsgroup groupingConceptId="166456">
				<br></br>
				<div id="schoolAndWork2-2">
					<span>Start date</span><obs id="startDate" conceptId="166426" allowFutureDates="true"/>
				</div>
			</obsgroup>
		
			<obsgroup groupingConceptId="166457">
				<br></br>
				<div id="schoolAndWork2-3">
					<span>Start date</span><obs id="startDate" conceptId="166426" allowFutureDates="true"/>
				</div>
			</obsgroup>
		</obsgroup>
	</div>
		<br></br>
		<br></br>
	<span class="breakLine">******************************************************************************************</span>
	
	<div id="section3">
		<obsgroup groupingConceptId="166429">
			<div id="personalData3">
				<span>Name</span><obs id="name" conceptId="166424"/>
				<span>Surname</span><obs id="surname" conceptId="166425"/>
			</div>
			
			<obsgroup groupingConceptId="166430">
				<br></br>
				<br></br>
				<div id="schoolAndWork3-1">
					<span>Start date</span><obs id="startDate" conceptId="166426" allowFutureDates="true"/>
				</div>
			</obsgroup>
			
			<obsgroup groupingConceptId="166456">
				<br></br>
				<div id="schoolAndWork3-2">
					<span>Start date</span><obs id="startDate" conceptId="166426" allowFutureDates="true"/>
				</div>
			</obsgroup>
			
			<obsgroup groupingConceptId="166457">
					<br></br>
					<div id="schoolAndWork3-3">
						<span>Start date</span><obs id="startDate" conceptId="166426" allowFutureDates="true"/>
					</div>
			</obsgroup>
		</obsgroup>
	</div>
		<br></br>
		<br></br>
	<span class="breakLine">******************************************************************************************</span>

</div>

<ifMode mode="VIEW" include="false">
	<div>
		<span id="errorMessageSpan"></span>
	</div>
	<br></br>
	<div id="buttons">
		<submit submitClass="confirm right" submitCode="Save"/>
		<button id="cancelButton" type="button" class="cancel"><uimessage code="general.cancel"/></button>
	</div>
	<redirectOnSave url="/htmlformentryui/htmlform/editHtmlFormWithStandardUi.page?patientId={{patient.id}}&amp;encounterId={{encounter.id}}"/>
    </ifMode>

</htmlform>

Obsgroup with id 166429 is ConvSet and contains two members - two text field obses (166424, 166425).

Each parent obsgroup (166429) has three obsgroups children (ids: 166430, 166456, 166457). Each child is ConvSet and has one date field (166426).

When I save in ENTER mode for example concepts 166424, 166425, 166456 (in div with ‘section1’ id) it is saved in database and displayed on UI correctly. Then if I fill in EDIT mode the second value of this group (166456) it is also saved and displayed correctly.

Another case (with issue): I save concepts 166424 and 166425 in EDIT mode in another section (div with ‘section2’ id). This is saved and displayed correctly. Then I want also save concepts e.g. 166430 and 166456 in the same section and in this case it is not saved correctly in database because obs with concepts 166430 and 166456 do not have assigned parent obs as obsgroup hence values are not displayed correctly on UI.

Looks like something issues with saving nested obsgroups in EDIT mode but only in case when none of nested obsgroup member is not saved yet. Probably it is not caused by repeat/template tags.

I am aware that it might be not clear enough because it is a bit complex, so the easiest way is try to load above form and mentioned concepts and test it.

@druchniewicz thanks for the clarification. I wanted to follow-up so that this doesn’t end here, but I’m not sure when I or others will have an opportunity to look in to it. At this point, I’d encourage you to create a ticket in the Htmlformentry project in JIRA, and mention both me and @mogoodrich on the ticket, and this will make this available for someone when available to investigate and fix.

The next step, if you are able to take any of this on, would be to create a unit test in the HFE module that contains the above form (or comparable) and demonstrates the failure that you report above with test failures in the current code.