2

I have a simple form that get code then display his libelle, I added a validator bean that check if the code exist. My problem is I can't display the error message whith when the code doesn't exist.

Here is the code:

test.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head><title>Test</title>
</h:head>
<body class="bodyMain">

<h:form>        
    <h:panelGrid columns="3">
        <h:outputText value="Code: " />
        <h:inputText id="code" value="#{myBean.code}"
            validator="#{myBean.validateCode}">
            <f:ajax execute="@this" render="libelle" listener="#{myBean.setLibelle()}"/>
        </h:inputText>
        <h:message for="code" style="color:red"/>
    </h:panelGrid>
    <h:panelGrid id="libelle" columns="2">
        <h:outputText value="Libelle: " />
        <h:outputText value="#{myBean.libelle}" />
    </h:panelGrid>
</h:form>       

</body>
</html>

MyBean.java

@ManagedBean
@ViewScoped
public class MyBean implements java.io.Serializable{
    private static final long serialVersionUID = 1L;

    private String code="";
    private String libelle="";

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code=code;
    }       

    public String getLibelle() {
        return this.libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle=libelle;
    }

    public void setLibelle() {
        if (code.compareTo("1")==0)
            libelle="One";
        else
            libelle="";
    }

    public void validateCode(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
        String code = (String)value;
        if (code.compareTo("1") != 0) {
            FacesMessage message = new FacesMessage("Code doesn't exist");
            throw new ValidatorException(message);
        }
    }

}

Thank you for your help to resolve this problem

1 Answer 1

4

You're not updating the <h:message> component by the <f:ajax>. You need to give the <h:message> an id and include it in the <f:ajax render>.

<h:inputText id="code" value="#{myBean.code}" validator="#{myBean.validateCode}">
    <f:ajax execute="@this" render="libelle codeMessage" listener="#{myBean.setLibelle()}"/>
</h:inputText>
<h:message id="codeMessage" for="code" style="color:red"/>

Unrelated to the concrete problem, don't initialize properties to empty strings. Let them by default null. Also, comparing objects should be done with equals() method, not with compareTo(). Finally, using a dropdown list with all available values instead of an input field would be more user friendly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.