My following code is not working as per expectation. The validation does not work as expected.
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>My Registration Page</title>
<link href="stylesheet/reset.css" rel="stylesheet" type="text/css" />
<link href="stylesheet/style.css" rel="stylesheet" type="text/css" />
<link rel="icon" type="image/png" href="images/icons/favicon.png" />
<script src="script/script.js" />
</h:head>
<h:body>
<h:outputText id="progress_bar" styleClass="progress-bar" />
<a4j:jsFunction name="login" action="#{loginBean.validateUser}" />
<div id="login-container">
<div class="login-logo">
<img src="images/logo.png" />
</div>
<f:view>
<div id="loin-form">
<h1>
Log in to your account or <a href="#">sign up</a> to Get Started
</h1>
<h:form>
<h:inputText id="userName" value="#{loginBean.userName}"
label="User Name" required="true" class="txtFld">
<f:validator
validatorId="com.coinfling.validation.LoginBeanValidator" />
</h:inputText>
<h:message for="userName" style="color:red"></h:message>
<h:inputSecret id="passWord" value="#{loginBean.passWord}"
label="Password" required="true" class="pswrdFld">
<f:validator
validatorId="com.coinfling.validation.LoginBeanValidator" />
</h:inputSecret>
<h:message for="passWord" style="color:red"></h:message>
<p>
<a4j:commandLink id="forgotPasswordLink" lable="Password"
value="Forgot Your Password? " />
<a4j:commandButton id="loginButton" value="Sign In"
onclick="login();" styleClass="sign-btn" />
</p>
</h:form>
</div>
</f:view>
</div>
</h:body>
</html>
My simple validation class code is below
package com.coinfling.validation;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
@FacesValidator("com.coinfling.validation.LoginBeanValidator")
public class LoginBeanValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent toValidate,
Object value) throws ValidatorException {
FacesMessage message = new FacesMessage("*UserName is not in Correct Format");
throw new ValidatorException(message);
//
// if(toValidate.getId().equals("userName"))
// {
// FacesMessage message = new FacesMessage("*UserName is not in Correct Format");
// throw new ValidatorException(message);
// }
// else if(toValidate.getId().equals("passWord"))
// {
// FacesMessage message = new FacesMessage("*Password is not in Correct Format");
// throw new ValidatorException(message);
// }
}
}
As you can see my validation i am just thrown an exception for whatever gets input. The required field is also not working. No error message is printed. What am i doing wrong ?
Kind Regards
You’re not actually submitting the form. You’re basically invoking a standalone bean method separately. That’s a huge difference. To execute validation on the form, you have to submit the form.
Replace the wrong
a4j:jsFunctionapproach:by the normal form submit approach:
I’m not sure why you used
a4j:jsFunction, but whatever you thought to benefit from thea4j:jsFunction, has definitely to be solved in a different way.Don’t forget to add a
render="@form"to update the form on complete of the ajax request, so that all messages are been shown: