validate global property on saving

Hi there!

is it possible to validate a global property before saving it?

Specifically in the legacy UI a user can configure global properties but if I have a GP for example an identifier prefix that should follow a certain regex pattern, how would I enforce that? I would like it to fail on saving it if it does not match my regex.

I have a class RadiologyProperties which has methods returning the GPs which is where I can enforce such a regex but failing here means that I am further away from the actual cause. It will only fail whenever the GP is requested and this might not even be by the user who configured the faulty value.

You should be able to register a Global Property validator as a spring bean and the API will invoke it when saving a global property

thanks @wyclif I will look into that! but this would mean that the validator is called on any save of a global property, regardless from which module.

I guess Ivo needs core to support validation of global properties matching a regexp by configuration, instead of having to code a specific validator. It would need additional metadata though (global property types).

Other global properties types that can be useful : url, e-mail address, decimal number, boolean …

1 Like

@lluismf yes! exactly what I would wish for :slight_smile: but I guess that would be a feature that needs to be implemented, right?

For now that is not supported, but you can do the validation in a module

@wyclif and @lluismf I found something that might work with your help :smile:

so as background: openmrs 2.0.0 SNAPSHOT Build 001ba8 legacy ui

There are global properties that in the legacy UI have radiobuttons so the admin can only choose between true/false. This lead me to the:

https://github.com/openmrs/openmrs-core/tree/2.0.x/api/src/main/java/org/openmrs/customdatatype/datatype

So I understand that by setting the global_property.datatype to the custom datatype org.openmrs.customdatatype.datatype.BooleanDatatype the radiobuttons show up.

Now there is also a RegexValidatedTextDatatype and the datatype_config which would set its regex pattern if I am not mistaken.

I added a GP

*************************** 1. row ***************************
         property: radiology.regex
   property_value: x
      description: NULL
             uuid: fe898a34-1ade-11e1-9c71-00248140a5eb
         datatype: org.openmrs.customdatatype.datatype.RegexValidatedTextDatatype
  datatype_config: ^[012]$
preferred_handler: NULL
   handler_config: NULL

a new row is added in the UI for this new GP, its a text box. But I can save whatever I want :frowning:

I also tried

  • ConceptDatatype
  • DateDatatype
  • ProviderDatatype

which all worked nicely and validate the entry before saving to the DB :slight_smile:

What am I missing? The RegexValidatedTextDatatype should be able to validate my required pattern.

thank you guys!!

I don’t have the code to look at, but can you debug RegexValidatedTextDatatype ? Maybe it’s not implemented, or it’s a bug. Also you may create a unit test showing that it doesn’t work.

@teleivo I agree and that sounds like the better solution

got it :slight_smile: @lluismf it wasnt implemented like you said.

I opened a feature request at https://issues.openmrs.org/browse/LUI-81 and have a PR ready https://github.com/openmrs/openmrs-module-legacyui/pull/49 hope you like it!