cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Is it possible to add parameters to constraint error messages within custom constraints

Former Member
0 Kudos
993

I created a custom constraint, extending attribute constraint. I would like to add some custom parameters to the error message during run time. How to do it?

Thanks a lot, Heiko

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Hi Heiko,

In order to implement dynamic validation messages you need to use the ConstraintValidatorContext that is passed to the isValid() method. Example based on the Hybris 123 trail should look something like this

 @Override
 public boolean isValid(final String value, final ConstraintValidatorContext context)
 {
     context.disableDefaultConstraintViolation();
     context.buildConstraintViolationWithTemplate("My custom error message").addConstraintViolation();
     return value == null || value.isEmpty() || !value.toLowerCase().startsWith("lorem ipsum");
 }

In this case failed validation will result in "My custom error message" message. Also you might need to make sure that Error message field of the constraint is empty. To do that go to BackOffice -> System -> Validation -> Constraints and in your constraint delete everything in the 'Error message' field.

Best regards,

Answers (1)

Answers (1)

Former Member
0 Kudos

thanks. it works. the problem was the message-entry at the constraint. I considered it as a fallback, but it isnt.