Validation message for GWT component

Hi,

I have created a custom GWT ‘rating stars’ component, using the instructions on Creating a GWT component - CUBA Platform. Developer’s Manual. I also have created a screen, where I use this rating field to enter a value for an entity attribute (‘score’).

Now, I want to validate if one or more stars have been selected. If no stars are selected, a validation message should pop up (and the border of the field should be marked red). This should work the same as when a required text field has been left empty. I already made the ‘score’ attribute mandatory, but this doesn’t seem to do anything: I can still save the entity while no star is selected (score = 0).

What’s the best way to achieve this validation?

Hi,

The mentioned topic describes how to create a GWT component on client-side that is connected to a Vaadin component on server-side. Such component doesn’t obtain any validation automatically, according to the data model, as the CUBA components do.

I suppose that the following topic will be useful to you: Integrating a Vaadin Component into the Generic UI.

Even if you integrate a Vaadin component into generic UI, i.e. create a CUBA component based on it, this case will be reproduced as 0 doesn’t equal to null, i.e. an entity attribute (score) has a value.

I would recommend adding the Min validation:

In this case, you don’t need to integrate a Vaadin component into generic UI.

Regards,
Gleb

Thanks for the suggestions. I’ve tried to add the Min validation:

Validation

After setting the validation, I’m still able to save the entity without entering a value, resulting in a score of 0 being saved:

Score_0

This is a screenshot of the controller:

Controller

Any idea why this isn’t working?

Could you please attach a demo project, so I can investigate a problem?

Hi @gorelov, here’s a link to a demo project with the ratingfield: https://we.tl/t-e7vqFxsSr9
In this demo project, you can see it’s still possible to save with value 0, although validation min=1 is set.

Hi,

Thank you for the demo project. Sorry that I mislead you, even for bean validation to work automatically, you need to integrate a Vaadin component into the generic UI.

Alternatively, you can validate the particular screen:

@Override
    protected void validateAdditionalRules(ValidationErrors errors) {
        if (getEditedEntity().getScore() < 1) {
            errors.add("Score must be greater than 0");
        }

        super.validateAdditionalRules(errors);
    }

Regards,
Gleb

Thanks, that code example you gave did the trick!