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).
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.
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.
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.
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);
}