Custom Constraint not working with ElementType.FIELD


We place json content in string fields and would like to help the ui-users with asserting that their input is valid json.

Following the doc and blog, I was able to quickly create a custom “class-level” annotation that does the job.

@Target({ ElementType.TYPE })
@Constraint(validatedBy = JsonFieldsValidator.class)
public @interface JsonFields {...}

However, when changing the annotation to ElementType.FIELD, I did not get the expected validation error messages when saving the changes. I did of course move the annotation from the entity to the corresponding String field in the entity.

@Target({ ElementType.FIELD })
@Constraint(validatedBy = JsonFieldValidator.class)
public @interface JsonField {...}

Are custom annotations also supported for fields? If yes, what is wrong with my code?


One thought: Does it have to do with the groups?

I annotated the String field with

@JsonField(groups = UiCrossFieldChecks.class)

The annotation is not a CrossFieldCheck, it concerns just one String field…

Yes, definitely the groups.

Annotation for single field validation:

@JsonField(groups = {UiComponentChecks.class})

@Team: Thanks again for the good framework and good documentation - fun to work with!