I create a form by using Apache MyFaces library. The form is related to the jsf-page by a binding. Moreover I built a TestValidator-class which implements Validator-interface. My form got shown, I enter my input, validation gots triggered successfully. Unfortunately I am not able to display corresponding FacesMessage. I guess I am not able to tell JSF 'Please rerender h:messages after throwing the validator-Exception'.
What went wrong? Thanks in advance.
@FacesValidator("TestValidator") // class TestValidator
public class TestValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent uiComponent, Object value) throws ValidatorException {
System.out.println("TestValidator.validate: " + value);
if (((String) value).equals("2")){
FacesMessage fm = new FacesMessage();
fm.setSeverity(FacesMessage.SEVERITY_ERROR);
fm.setSummary("TestValidator FEHLER");
fm.setDetail("TestValidator FEHLER");
throw new ValidatorException(fm);
}
}
}
<!-- JSF-Snippet -->
<t:div id="formContainer" binding="#{formsbuilder.form}" />
private Div createFacesMessagesDiv() { // FormsBuilder-Bean-Snippet for creating the container for the FacesMessage
facesMessagesDiv = new Div(); // CORRESPONDING BEAN PROPERTY
facesMessagesDiv.setId("facesMessagesContainer");
facesMessagesDiv.setStyle("color: blue; border: 3px solid green;");
HtmlOutputText introText = new HtmlOutputText();
introText.setId("facesMessagesIntroText");
introText.setValue("FacesMessages - Start: ");
facesMessagesDiv.getChildren().add(introText);
HtmlMessages fms = new HtmlMessages();
fms.setId("facesMessages");
facesMessagesDiv.getChildren().add(fms);
HtmlOutputText outroText = new HtmlOutputText();
outroText.setId("facesMessagesOutroText");
outroText.setValue("FacesMessages - Ende!");
facesMessagesDiv.getChildren().add(outroText);
return facesMessagesDiv;
}
private HtmlAjaxCommandLink createSaveFormButton() { // saveButton after hitting this button, facesMessages should show up;
HtmlAjaxCommandLink saveFormButton = HtmlRendering.createGeneralButton("saveForm" + currentSubForm.getIdAsString(), /* ID */
"#{resources.labels['formsgenerator_saveForm']}",
"#{formsbuilder.submitForm}",
null, /* action */
null, /* actionReturnType */
null, /* onComplete */
"", /* styleClassValueExpression */
"", /* imageValue */
"modifiedContainerDiv");
saveFormButton.setReRender(facesMessagesDiv); // CORRESPONDING BEAN PROPERTY
saveFormButton.setValueExpression("oncomplete", HtmlRendering.createValueExpression("afterGeneralSavingSubForm()");
return saveFormButton;
}