Wednesday, April 29, 2020

Validating a field against existing form data

Here is a scenario that a customer asked us recently: can you validate a field to make it invalid if its value already exists in data saved in the database?

The answer is that it is possible! Here is how, assuming you have a field called "Job" in your form that you must validate this way. First, create a service like this:



Here:
  • The path `/app/form` must be adjusted to your app/form names.
  • The path, in the XML, `section/job` must be adjusted to your section name/control name.
This service will call the Orbeon Forms built-in search service, asking to retrieve specifically the value of the "job" form control.

Mark the "Job" field as searchable by selecting the "Show in search" checkbox in the Control Settings dialog, so that the field is indexed and searchable in the database.

Then create an action that calls this service upon form load:



Store the service response into a dataset called "jobs":



Finally, add a constraint to say that you don't want the value of the field to belong to that set:



And that's it! The service will run on form load, store the list of all existing job values, then validate the current job value against all the existing values.



We should note that this has limitations, in particular, if another user saves new form data in the meanwhile, then the constraint will not be synchronized. An improvement could be to re-run the service before saving.

No comments:

Post a Comment