Spring Boot Custom Validation Message Not Showing on UI

34 Views Asked by At

Created a custom validator interface and class that is currently working as expected. @MinMaxInv checks that the inv level being set for the Parts entities is >= min && <= max. However the custom message is not displaying anywhere in the console or UI when testing. I've added the annotation to the abstract class and also the classes that extend the abstract but that doesn't change the results. I have a default message in the interface, and have also tried adding (message="messagetext") to the annotation in the classes, neither message appears.

Total noob to Spring Boot so any help would be appreciated!

Validator class

public class MinMaxInvValidator implements ConstraintValidator<MinMaxInv, Part> {
    @Override
    public void initialize(MinMaxInv constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(Part part, ConstraintValidatorContext constraintValidatorContext) {
        if(part.getInv() > part.getMax() || part.getInv() < part.getMin()){
/*            constraintValidatorContext.disableDefaultConstraintViolation();
            constraintValidatorContext.buildConstraintViolationWithTemplate("Inventory must be between min and max").addConstraintViolation();*/
            return false;
        }


        else return true;
    }

Interface

@Target({ElementType.FIELD, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = MinMaxInvValidator.class)
public @interface MinMaxInv {
    String message() default "Inventory must be between min and max";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

}

Annotation with message added in class extending the abstract class, also tried with no message added to test default in interface

@Entity
@DiscriminatorValue("2")
@MinMaxInv(message = "The inventory '${validatedValue.inv}' must be between '${validatedValue.min}' and '${validatedValue.max}'")
public class OutsourcedPart extends Part{
0

There are 0 best solutions below