Form controls based on patient attributes

Hello OpenMRS community. We are currently implementing a Bahmni instance for certain demographics with custom forms. I want to ask if it is possible to show/hide parts of a form based on patient attributes using Form Builder. For example, if a patient is male, they don’t need to see fields such as “Are you currently pregnant?” I learned of the getPatient() method from the Form Builder docs, but I am unsure how to use it. Any help is much appreciated.

What version of Bahmni are you using ?

Hi @ravinderkabli_tw. I’m running Bahmni Standard 1.0.0 via Docker

The form context object exposes these APIs:

form.get(conceptName) - Get a control by concept name

form.getPatient() - Get a patient

objectcontrol.setHidden(boolean) - Hide/show a control

control.setEnabled(boolean) - Enable/disable a control

control.getValue() - Get a control’s value

control.setValue(value) - Set a control’s value

Your use case — attach this to the Form Load event:

function(form) {

var patient = form.getPatient();

if (patient.gender === 'M') {

    form.get('Are you currently pregnant?').setHidden(true);

} else {

    form.get('Are you currently pregnant?').setHidden(false);

}

}

Important:

  1. If using the Form Builder (Forms 2.0): Try form.getPatient() in a Form Load event script first. Test it to confirm the patient object is populated correctly.
  2. If getPatient() doesn’t work or returns incomplete data: Fall back to formConditions.js with the patient parameter for Forms 1.0.

Let me know if this helped.

Thank you! This worked. I am able to get gender using patient.gender

Other properties such as patient.age, patient.birthdate etc are also available.