Reputation:
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
required="true" validator="#{RegistrationBean.checkFirstName}">
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
Upvotes: 0
Views: 1250
Reputation: 81667
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!
Upvotes: 3