Handling Validation Error Messages

Dears,

I am trying to add a validation to my entity disallowing the user to enter duplicate entries (based on some fields combination).
For this, I have created an entity listener and implemented the 2 methods onBeforeInsert() and onBeforeUpdate(), which read existing data from the database.

Based on that, I want to display an error message to the user if he is trying to enter duplicate info.

What is the best way to do this? Could you please share by providing some pseudo-code or an example?

I have read some related questions on the forum as well as the part related to Exception Handling in the guide, but couldn’t really manage to understand where to define the exception and how to call it, use it and throw it.

Your usual help is highly appreciated.

Shady Hanna

Hi Shady,

if i get it correctly, this requirement should be solvable through the usage of a unique constraint on the database that combines all the fields of your entity. A unique constraint can be configured through studio (see attached screenshot).

The error message can be adjusted through the mechanism of uniqueConstraintViolation (see this topic e.g.: https://www.cuba-platform.com/discuss/t/validation-message-for-unique-constraint).

Of course you can implement that by hand (through EntityListeners) but that not really required if i get it right.

An example of Exception Usage can be found in this example project cuba-example-xero-api-integration i created a while ago. The exception class is
XeroConfigurationException as well as it’s usage: XeroServiceBean.

Bye
Mario

unique-constraint-example

Hi Mario,

Thanks for your prompt reply.

I created the unique constraint on the entity level, but I am failing to customize the error message.
I have added the unique contraint name in messages.properties (tried all messages.properties fiels under web, under gui, …) but always getting “unique constraint violation occurred”. I am not sure what I am missing. I tried restarting the app server with no luck.
Any suggestion?

As for the second part related to the entity listeners, I understand it is not needed for this unique validation specifically, but I need to implement many validations in my billing application soon.
I will be reviewing the example you shared and get back to you.

Shady

Hi,

As for the second part related to the entity listeners, I understand it is not needed for this unique validation specifically, but I need to implement many validations in my billing application soon. I will be reviewing the example you shared and get back to you.

As a side note, in 6.4 (at the end of the month) there will be a new mechanism using BeanValidation that might change the way how validation can be done. I just wanted to point that out, not that you do this implementation twice :slight_smile:

Thanks Mario for pointing out that.

Any idea regarding the first part?

I’ve just added examples of specifying a unique constraint message and handling an error message from an entity listener to the Data Manipulation sample.

Hope this helps. Have a look also at this topic.