How to implement multiple JSR-303 validation messages for the same @Constraint?

Steve Chambers

I'm using JSR-303 validation (hibernate-validator) for an entity with a few different rules to be applied. Would rather not stack up multiple @Constraint annotations for these and instead use a single one, e.g. @MyEntityConstraint.

Problem is there really needs to be a different message for each type of validation failure but the message seems inextricably associated with the annotation:

public @interface MyEntityConstraint {
    String message() default "A single, unchangeable message per constraint???";
    // ...
}

Is there any way round this or am I doomed to have:

@MyEntityConstraint1
@MyEntityConstraint2
// ...
@MyEntityConstraintn
@Entity
public class MyEntity {
    // ...
}
Steve Chambers

As suggested by Hardy, this can be done simply enough using ConstraintValidatorContext - as below:

@Override
public boolean isValid(MyEntity myEntity, ConstraintValidatorContext context) {
    // Disable default ConstraintViolation so a customised message can be set instead.
    context.disableDefaultConstraintViolation();

    return checkConstraint1(myEntity, context)
           && checkConstraint2(myEntity, context)
           //...
           && checkConstraintn(myEntity, context);
}

// Note: A private method for each constraint decreases the cyclomatic complexity.
private boolean checkConstraint1(MyEntity myEntity, ConstraintValidatorContext context) {
    // Default validity is true until proven otherwise.
    boolean valid = true;

    if (/*<<Insert constraint #1 conditions (about myEntity) here>>*/) {
        valid = false;
        context.buildConstraintViolationWithTemplate(
           "<<Insert constraint #1 failure message here>>").addConstraintViolation();
    }

    return valid;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Nested validation groups, Spring, JSR 303

From Dev

kotlin data class + bean validation jsr 303

From Dev

JSR-303 Bean Validation Collection Values

From Dev

bean validation (JSR 303) for primefaces not working

From Dev

How to break coupling between bean and validator with Bean Validation (JSR303)?

From Dev

Spring Boot how to return my own validation constraint error messages

From Dev

JSR 303 bean validation unit testing with Mockito and Autowiring

From Dev

Can JSR 303 Bean Validation be used with Spring Data Rest?

From Dev

Spring mvc controller jsr 303 basic list validation

From Dev

Wildfly: ExceptionMapper not triggered with RestEasy JSR-303 Bean Validation

From Dev

Spring JSR-303 bean validation not working, empty bindingresult

From Dev

JSR-303 validation groups define a default group

From Dev

GWT JSR303 Validation, validate method OR use custom annotations

From Dev

Spring mvc controller jsr 303 basic list validation

From Dev

Spring + JSF2 + JSR 303 bean validation

From Dev

spring boot jsr 303 validation with nested array path fails

From Dev

Conditional JSR-303 validation based on enclosed class type

From Dev

BindingResult not working in Spring 3.2.8 using JSR-303 BEAN VALIDATION

From Dev

Spring MVC: use different JSR-303 validators on the same bean?

From Dev

Spring MVC: use different JSR-303 validators on the same bean?

From Dev

knockout validation multiple messages

From Dev

How to handle multiple websocket messages at the same time?

From Dev

How to handle multiple websocket messages at the same time?

From Dev

How to combine JSR-303 and Spring Validator class in a service layer?

From Dev

Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

From Dev

Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

From Dev

How to resolve dependencies in JSR-303 custom validator

From Dev

implement symfony/constraint/validation as assert in laravel/doctrine

From Dev

implement symfony/constraint/validation as assert in laravel/doctrine

Related Related

  1. 1

    Nested validation groups, Spring, JSR 303

  2. 2

    kotlin data class + bean validation jsr 303

  3. 3

    JSR-303 Bean Validation Collection Values

  4. 4

    bean validation (JSR 303) for primefaces not working

  5. 5

    How to break coupling between bean and validator with Bean Validation (JSR303)?

  6. 6

    Spring Boot how to return my own validation constraint error messages

  7. 7

    JSR 303 bean validation unit testing with Mockito and Autowiring

  8. 8

    Can JSR 303 Bean Validation be used with Spring Data Rest?

  9. 9

    Spring mvc controller jsr 303 basic list validation

  10. 10

    Wildfly: ExceptionMapper not triggered with RestEasy JSR-303 Bean Validation

  11. 11

    Spring JSR-303 bean validation not working, empty bindingresult

  12. 12

    JSR-303 validation groups define a default group

  13. 13

    GWT JSR303 Validation, validate method OR use custom annotations

  14. 14

    Spring mvc controller jsr 303 basic list validation

  15. 15

    Spring + JSF2 + JSR 303 bean validation

  16. 16

    spring boot jsr 303 validation with nested array path fails

  17. 17

    Conditional JSR-303 validation based on enclosed class type

  18. 18

    BindingResult not working in Spring 3.2.8 using JSR-303 BEAN VALIDATION

  19. 19

    Spring MVC: use different JSR-303 validators on the same bean?

  20. 20

    Spring MVC: use different JSR-303 validators on the same bean?

  21. 21

    knockout validation multiple messages

  22. 22

    How to handle multiple websocket messages at the same time?

  23. 23

    How to handle multiple websocket messages at the same time?

  24. 24

    How to combine JSR-303 and Spring Validator class in a service layer?

  25. 25

    Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

  26. 26

    Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

  27. 27

    How to resolve dependencies in JSR-303 custom validator

  28. 28

    implement symfony/constraint/validation as assert in laravel/doctrine

  29. 29

    implement symfony/constraint/validation as assert in laravel/doctrine

HotTag

Archive