tags:

views:

737

answers:

1

i am using a4j for checking username exists or not for onblur event it displays the error messages when user already exists but ,if i click on submit button after displaying the error message it gets submitted when it comes to the required=true for inputtext it doesn't get submitted


public void checkFirstName(FacesContext facesContext, UIComponent component, Object value)
{

 String name = (String) value;

     if(name.trim().length()==0){
         System.out.println("Name *******");

         String message = bundle.getString("Name_Required");
  FacesContext.getCurrentInstance().addMessage("Reg:firstName",
                new FacesMessage(FacesMessage.SEVERITY_ERROR, message,message));    


     }                  
     if(name.trim().equalsIgnoreCase("tom")){

         String message = bundle.getString("Name_exists");
  FacesContext.getCurrentInstance().addMessage("Reg:firstName",
                new FacesMessage(FacesMessage.SEVERITY_ERROR, message,message));    

 }

  }

could anyone suggest me where i went wrong

+3  A: 

Hi,

With the JSF code, it would be easier to help you. So I suggest that you have something like that in your JSF page:

<h:inputText id="firstName" ... validator="#{aBean.checkFirstName}">

The problem on your Java code is that you do not throw any ValidatorException when an error occurs. Thus, your code must be:

public void checkFirstName(FacesContext facesContext, UIComponent component, Object value) {
    String name = (String) value;
    if ((name == null) || name.trim().length() == 0) {
        System.out.println("Name *******");
        String message = bundle.getString("Name_Required");
        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message)); // Throw the required error
    }
    if (name.trim().equalsIgnoreCase("tom")) {
        String message = bundle.getString("Name_exists");
        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message)); // Throw the already exist error
    }
}

This way, if the value filled in this field is empty or already exists, a ValidatorException will be thrown, the user will get an error message (do not forget to add the <h:messages/> or <h:message/> component in your form), and the form will not be submitted!

romaintaz
thanks for your support my problem is resolved
If your problem is solved, please select my answer as the correct answer (if it is the case) (click on the icon below the 0 on the left)
romaintaz