@NotNull constraint behaving different on entity creation from UI and from v2 API


I have an entity say User with an attribute say group which has @NotNull constraint.

I am deriving and setting this group in UserEntityListener in method beforeInsert. This works when I create an user from UI (form does not have group field) but throws error that ‘group is required’ when I create using v2 API and do not pass group as it is supposed to be inserted by listener before actually saving to db.


By design, there is a difference between validation in the generic REST-API and UI layer. REST-API validates all the data before sending it to the middleware, because there is no way to change the generic REST-API methods.

In your case you should create a custom REST-API controller that will not validate certain fields that are filled later in the middleware code. See: https://doc.cuba-platform.com/manual-6.8/rest_api_v2_custom_controllers.html

If you always set group in the entity listener, probably, there is no need to use BeanValidation, just use required attribute in UI if it is required in some use cases.

Please note, that BeanValidation is not used in middleware layer, it is a mechanism of client layers: web / rest-api.

1 Like