1

I'm wondering if such a thing is possible. I have a general DTO class called "Device". Device holds ID and IP. I've set a inner regexp validation to verify if the IP is entered correctly.

But now I want to have another validation of type "verify if this IP is not used by another device"(for both update and create cases).

For this I have written a custom Type orientated ConstraintValidator, where I accomplish the task of checking if the is no other device with the given IP.

But from this point I wanted to go further, and to have several checks and several response error messages from one ConstraintValidator, doing random checks on the Device object.

On this point I couldn't find if I could redefine validator's error message. Is this possible?

2
  • If i understood you correctly then this is answear for you. Commented Aug 27, 2018 at 11:19
  • Thanks. Will try Commented Aug 27, 2018 at 11:36

2 Answers 2

4

You can do this using the following function I use for my own API :

public static void setNewErrorMessage(String newErrorMessage, ConstraintValidatorContext context) {

    context.disableDefaultConstraintViolation();
    context.buildConstraintViolationWithTemplate(newErrorMessage)
            .addConstraintViolation();
}

The context object here is passed in the isValid function of any ConstraintValidator implementing class.

This way you'll be able to do something like :

if(hasError1()) {
     setNewErrorMessage(ERROR1_MESSAGE, context);
     return false;
}

if(hasError2()) {
     setNewErrorMessage(ERROR2_MESSAGE, context);
     return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Was one foot there:) Formed the Constraint, but didn't use the 'addConstraintViolation'. This one did the trick. Thanks.
1

You should use the Hibernate Bean Validation API, to build something like the code below.

HibernateValidatorConfiguration configuration = Validation
        .byProvider( HibernateValidator.class )
        .configure();

ConstraintMapping constraintMapping = configuration.createConstraintMapping();

constraintMapping
    .type( Car.class )
        .property( "manufacturer", FIELD )
            .constraint( new NotNullDef() )
        .property( "licensePlate", FIELD )
            .ignoreAnnotations( true )
            .constraint( new NotNullDef() )
            .constraint( new SizeDef().min( 2 ).max( 14 ) )
    .type( RentalCar.class )
        .property( "rentalStation", METHOD )
            .constraint( new NotNullDef() );

Validator validator = configuration.addMapping( constraintMapping )
        .buildValidatorFactory()
        .getValidator();

Take a look at http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-programmatic-api

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.