0
votes

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;
}
1
Can i ask why you are building html elements in your backing bean? There is probably no need for this and it's bloating your code.Catfish

1 Answers

0
votes

It does not look like you are setting the message correctly.

You forgot to add the FacesMessage to the FacesContext.

Try this:

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");
        FacesContext.getCurrentInstance().addMessage(null, fm);
        throw new ValidatorException(fm); 
    }
}

Here's a more concise way to write a FacesMessage:

public void validate(FacesContext context, UIComponent uiComponent, Object value) throws ValidatorException {
    System.out.println("TestValidator.validate: " + value);
    if (((String) value).equals("2")){
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "TestValidator FEHLER", "TestValidator FEHLER"));
        throw new ValidatorException(fm); 
    }
}