0

Unable to show faces message in xhtml page - its showing in console. In forgotPassword link, like to check if user exist or not

<h:outputText value="Enter User Name" />
<h:inputText  value="#{loginBean.technicianName}" required="true" 
    requiredMessage="user name is required" id="unameId"  >
    <f:validator validatorId="com.beans.UserNameAvailableValidator" />
    <f:ajax event="blur" render="username_message" />
</h:inputText>
<rich:message for="unameId" id="username_message"/>

bean code:

@FacesValidator("com.beans.UserNameAvailableValidator")
@RequestScoped
public class UserNameAvailableValidator implements Validator {
  UserdetailsDAO userdetailsDAO = null;

  @Override
  public void validate(FacesContext fc, UIComponent uic, Object value) throws ValidatorException {
    String userName = (String) value;
    userdetailsDAO = new UserdetailsDAOImpl();
    try {
      if(userdetailsDAO.getUserdetails(userName)!= null) {
        System.out.println("user exist");
      } else {
        throw new ValidatorException(new FacesMessage("Username doesnot exist "));
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}
Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
  • did you try tag ? – mehmet cinar Apr 19 '14 at 21:04
  • Your validator will display message in `rich:messages`. Validator doesn't know that message is exact for unameId. – Vasil Lukach Apr 20 '14 at 00:47
  • @mehmet cinar i tried with h:message but it is not working – user3550816 Apr 20 '14 at 06:39
  • `h:message ` cannot help you. You can add `label` attribute in your `h:inputText` and use it in validator: if label is defined then `FacesMessage` is for UI component with this label, if label not defined then message is general (for view, not for specific component). – Vasil Lukach Apr 20 '14 at 15:47
  • possible duplicate of [How to add a message to a specific component from java](http://stackoverflow.com/questions/5288267/how-to-add-a-message-to-a-specific-component-from-java) – danizmax Oct 02 '14 at 05:28

1 Answers1

0

You are only creating the message, you also need to add put it somewhere (the message does not work the same way as exception), in this case you add it to the context:

FacesContext fc = FacesContext.getCurrentInstance();
UIComponent root = fc.getViewRoot();
UIComponent component = root.findComponent("unameId");
fc.addMessage(component.getClientId(fc), new FacesMessage("Username does not exist."));
Makhiel
  • 3,874
  • 1
  • 15
  • 21