Unfetched attribute in a constraint validator results in null pointer exception

Hello Team,

I have a problem retrieving an unfetched attribute the code is:

static public class EmployeesGroupEntityValidatorImpl implements ConstraintValidator<EmployeesGroupEntityValidator, EmployeesGroup> {

    public void initialize(EmployeesGroupEntityValidator constraintAnnotation) {

    public boolean isValid(EmployeesGroup value, ConstraintValidatorContext context) {
        Boolean flag = true;
        if (!value.sector.country.getId().equals(value.country.getId())){
                modifyConstraintValidatorContext(context, "{msg://com.company.vp.entity/EmployeesGroup.errors.sectorE1}");
                flag = false;

        return flag;

when the entity is in a new state the constraint validation works fine, but when i try to edit the entity a null pointer exception happens in the following line:

if (!value.sector.country.getId().equals(value.country.getId())){

because the “country” in “value.sector.country.getId()” is null, i think because it’s unfetched.
is there a way to apply the validation while editing the entity.

Hello Team,

can i get a feedback please, is there a way to apply the cross attribute validation if the entity is not new.


Bean validation constraints are executed on the client layer and you should check if required attributes are loaded. You can do this using isLoaded and isLoadedWithView methods of EntityStates bean.

If you really need to load those attributes for validation you could reload the instance with the desired view using DataManager.

Another way to validate entities in UI is postValidate method: https://doc.cuba-platform.com/manual-6.9/abstractWindow.html#abstractWindow_postValidate

Please note that it is a free forum and we do our best to share knowledge as soon as possible. If you need additional assistance I’d recommend that you check our commercial support options.

Hello Yuriy,

Thank you for your reply, i am aware that this is a free forum. I do apologize for replying after 4 days from my initial post asking for a feedback.

I was under the impression that the model and the bean validation both are in the Middleware and not the client tier, i am prefer data aware model, where validation happens there whenever possible.

if i use the post validate, does that ensure the entity is fully loaded even in editing situation?
can you please confirm my understanding of the bean validation place, is it the client tier or the middleware tier?

Thanks in advance.

Bean Validation is performed in client tier and not in transaction, it enables you to prevent sending incorrect data to middleware.

Client tier means that code is executed on web server where transactions are not available and only small amount of data loaded. If you need additional attributes you can always load them during validation with DataManager.

Also, it means that you can have different validation rules depending on context. Unfortunately, when you commit data to DB there is no UI context available.

1 Like

If you need transactional scope for validation take a look at Entity Listener functionality.