Summary
This edit check verifies that for a medical history form, the chosen value of ongoing is consistent with end date.
<iframe width="640" height="336" src="https://www.loom.com/embed/590ce3bdd7c94b90a4ad9bfcdc1dc2a5%22 frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
Formal Expression
var ONGOING_SAS_FIELD_NAME = 'MHONGO'; var endDate = itemJson.item.value; var repeatKey = itemJson.item.itemGroupRepeatKey; logger('endDate '+endDate); logger('repeatKey '+repeatKey); var ongoingItem = findFirstItemByName(formJson, null, ONGOING_SAS_FIELD_NAME, repeatKey); var ongoing = ongoingItem.value; logger('ongoing '+ongoing); var valid = true; if (ongoing.equalsIgnoreCase('true')) { if (endDate) { customErrorMessage('There can not be an end date if ongoing is true'); valid = false; } } else if (ongoing.equalsIgnoreCase('false')) { if (!endDate) { customErrorMessage('There must be an end date if ongoing is false'); valid = false; } } else { logger('unexpected ongoing value of ['+ongoing+']'); } return valid;
Here is an alternate implementation that is more sophisticated and may be useful for AEs
var ongoingVarName = 'AEONGO'; var endDateVarName = 'AEENDAT'; var outcomeVarName = 'AEOUT'; var ongoingYesValue = 'Y'; var acceptableOngoingOutcomeValues = ['FATAL','NOT RECOVERED/NOT RESOLVED','RECOVERING/RESOLVING','UNKNOWN']; var acceptableNotOngoingOutcomeValues = ['RECOVERED/RESOLVED','RECOVERED/RESOLVED WITH SEQUELAE']; var thisRepeatKey = itemJson.item.itemGroupRepeatKey; // logger('thisRepeatKey '+thisRepeatKey); var outcome = findFirstItemByName(formJson, '', outcomeVarName, thisRepeatKey).value; //logger('outcome '+outcome); var endDate = findFirstItemByName(formJson, '', endDateVarName, thisRepeatKey).value; //logger('endDate '+endDate); var ongoing = findFirstItemByName(formJson, '', ongoingVarName, thisRepeatKey).value; //logger('ongoing '+ongoing); var endDatePresent = endDate !== null && hasEndDate(endDate); //logger('endDatePresent '+endDatePresent); var errorMessage = null; if (ongoing == ongoingYesValue) { // logger('ongoing is '+ongoingYesValue); if (endDatePresent == true) { errorMessage = 'There can not be an end date for an ongoing AE'; } else if (acceptableOngoingOutcomeValues.indexOf(outcome) == -1) { errorMessage = outcome+' is not an acceptable outcome for an ongoing AE'; }else { // logger("valid: Ongoing is ["+ongoingYesValue+"], end date is NOT present, and the outcome "+outcome+" is in the list of values acceptable for ongoing conditions") } } else { // logger('ongoing ['+ongoing+'] is not '+ongoingYesValue); if (endDatePresent == false) { errorMessage = 'There must be an end date for a non-ongoing AE'; } else if (acceptableNotOngoingOutcomeValues.indexOf(outcome) == -1) { errorMessage = outcome+' is not an acceptable outcome for a non-ongoing AE'; } else { // logger("valid: Ongoing is not ["+ongoingYesValue+"], end date is present, and the outcome "+outcome+" is in the list of values acceptable for non-ongoing conditions") } } if (errorMessage != null) { customErrorMessage(errorMessage); return false; } return true; // true if at least the year is specified function hasEndDate(dateString) { var matches = dateString.match(/^\d{4}.*$/); // logger('regex against ['+dateString+'] matches? '+matches); return matches != null; }