on 2018 Feb 23 12:12 PM
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
Request clarification before answering.
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thanks. it works. the problem was the message-entry at the constraint. I considered it as a fallback, but it isnt.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.