Validation Rules: How to add validation rules for a form having category options to switch context


Tree view

Hi,
I have created a dataset for Covid-19 vaccination program. I have two tables as part of the form that is shown/hidden depending on the category option (Inreaches or Outreaches) that has been selected. The rest form remains the same. This context switching was done using custom Javascript by checking the category option IDs.
Problem:
When creating Validation rules, the rules are not aware of the context i.e whether Inreaches or Outreaches has been selected thus applying one validation rules for both scenarios at the same time. This makes it difficult to implement the correct validation rules.
Trial:
I’m imagining a scenario where I could apply the rules when the selection is Inreaches as;
if(isNotNull(#{Inreaches_id}),#{jhM95llxJFJ} + #{rdoD5B9Bz39} + #{PvlGmUrxr23},0)


This of course did not work since Validation rules does not allow passing category option.
Overriding Validation Buttons:
Tried to override onclick listener for the validation buttons such that values are compared with custom Javascript. I’m imagining this could be a viable solution but didn’t manage to achieve it.
Suggestions/Solutions:
Kindly share your thoughts on how this could be tackled.
Thanks :blush:

Hi @salad_guyo1

I’ve an idea but didn’t test it. How about creating a data element which will be assigned a value depending on which co is selected? Then in the validation rules you could create a nested if statement, if DE is inreaches then the if statement for inreaches else if (or else) DE is outreaches then the if statement for outreaches.

Hope it works! My other question for you please is aren’t we supposed to mention the co in the validation rules for example #{dataElemenUiD.categoryOptionsUiD}? Are you doing that as well?

Thanks!

Helw,
I honestly don’t know how to achieve the approach you suggested as for your question the validation rules documentation only shows support for DE.CatCombo i.e ${DE.CatOptionCombo}. The main challenge is, the CO is not directly associated with the data elements; It’s tied to the dataset through CatCombo, maybe ${Dataset.CatCombo.Cat.CO} would have helped but it seems not supported.
Thanks.

1 Like

Oh, right! I was thinking you could use at least one category option but I’ve another suggestion which I hope will work:

The scenario is that if the user selects ‘outreaches’ they’re not going to be able to fill in the fields for ‘inreaches’, right? Which means that if ‘inreaches’ is null it might be because ‘outreaches’ was selected but to make sure that ‘inreaches’ was actually not selected and left out null, the fields in ‘outreaches’ need to have a value. In other words using pseudocode, if(isNull(#{inreaches_id}) AND isNotNull(#outreaches_id)) then you’d put in the conditions for outreaches, else if(isNotNull(#inreaches_id) AND isNull(#outreaches_id)) then you’d put the conditions for outreaches, and finally if both are null if(isNull(#{inreaches_id} AND (#{outreaches_id}))) then the user didn’t insert any value at all.

See if that works please! Hope it does, thanks! :+1:

1 Like

Yeah,
exactly how I tried to approach but the system doesn’t support checking Category option IDs in our case (Inreaches/Outreaches). It only supports #{DataEelement_ID} or #{DE_ID.CatOptionCombo_ID} according to Data Validation - DHIS2 Documentation
Doing if(isNotNull(#{inreaches_id}),…) will result into invalid expression error because inreaches_id is neither data_element_id nor #{DE.CatOptionCombo}
NOTE
Still I’m saying this in the context of inbuilt validation rules.
Thanks

1 Like

Here I meant the first data elements from the inreaches and outreaches tables. ( I assumed the first data element would be called inreaches_id or outreaches_id

Sorry they’re not data elements. They’re category options tied to the dataset through CategoryCombination. (CatCombo>>>Category>>CatOptions(Inreaches and Outreaches)).
I think I’ll now have to change the hierarchy structure and make both inreaches and outreaches to be data element. That way the logic you suggested will work perfectly.
Thanks!

1 Like

Great! As long as you’d still be able to hide/show the tables based on which data element is selected as in your first post you mentioned how this is dependent on the catoption selection.

I’d keep everything the same and add an option set (Inreaches and Outreaches) which will help with the validation suggested above.

If the suggestion works please mark post as solved. Thanks!:+1: