Hi,
I have a small question concerning Hibernate Validator and the integration of it in a Struts 1.1 application.
We have a kind of ValidationUtil class where we can call the methods isValid() and getValidationErrors() on a POJO, which delegates to Hibernate ClassValidator.
I would like to integrate Hibernate Validator in the Struts form where my POJO resides.
In the validate() method of the ActionForm I call the validation and try to make suitable ActionErrors out of the information received by Hibernate Validaor (the InvalidValues[] array).
It's kind of like this:
Code:
public ActionErrors validate(ActionForm actionForm, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
DetailForm detailForm = (DetailForm)actionForm;
if (!detailForm.getPojo().isValid()) {
for (InvalidValue value : detailForm.getPojo().getValidationErrors()) {
errors.add(value.getProperty(), new ActionError(value.getMessage(), <LABEL-KEY>));
}
}
return errors;
}
Now, I already thought that the InvalidValue.getMessage() already gives back the I18N-ed message String in the correct language...but the Struts ActionError class expects a key.
I found out that when you get a ConstraintViolationException or IllegalStateException from Hibernate itself, you can get the messageKey out of the Exception.
From the InvalidValue, I cannot seem to get the message key ... But I thought I got that sorted out by specifying my own key.
Now ...
Next to every <html:text> tag and alikes, we have a <bean:message> tag to display a label for the field.
It's THAT I18N-ed label name that I want to include in the error message that I got back from Hibernate Validator, or that I have to create an ActionError with.
I don't want to check the "if property == this-name then take that-label-key" stuff. I want to be able to do it generically/dynamically.
Also ... I can't use the getPropertyPath method also, because all our labels on screen are already defined in property files and the keys do not match classnames and propertypaths. IMHO, I would like to use the label "name" from one key, and don't have to specify it every single time for a different property path.
Can anyone help me with this?
With JSF it's easy ... It works like a charm and takes the label- or for- attribute in consideration when building the error message, also there ... I use the EventListener so accually he does it before persisting it.
Here in Struts 1, it appears to need customization, and I want to do it before persisting it, but also on the screen in the validate() method of the ActionForm.
I only find information regarding the integration of Hibernate Validator in Struts 2.x and JSF applications...
Thanks for any information you can give me.
Kind regards,
Steve