Struts 2 how to get i18n messages from within a custom validator
Asked Answered
M

1

2

How is it possible to get the message from key in the custom validator ?! As mentioned in Struts 2 - reusing Custom Expression Validator you can get default message as :

public void validate(Object o) throws ValidationException {

    //Do some logic
    addActionError(getDefaultMessage());
}
Myongmyopia answered 20/1, 2015 at 20:16 Comment(3)
Use getMessage, btw downvote isn't mine.Alignment
I was asked by @Andrea Ligios to ask it in different question :( #28039327 I was mislead ;) Thanks for answer please send the answer so I can accept it!Myongmyopia
Upvoted both... Now it fits SOAparicio
A
2

Your custom validator should extend ValidatorSupport class, which has a convenient method getMessage(Object object) to get i18n messages set with key parameter.

So inside validate method instead of calling getDefaultMessage (which simple returns default message) call getMessage which will evaluate key parameter with additional messageParams.

public void validate(Object o) throws ValidationException {
    //Do some logic
    addActionError(getMessage(o));
}
Alignment answered 20/1, 2015 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.