Monday, March 28, 2011

Faces UI field validation

To validate an individual UI field from Faces Side(Mostly for POJO Trees) and dis-allow user to submit the form with Save button, 2 things you need to do
In JSFF, for the UI component, add a Validator Method as below

                <af:inputText value="#{node.Email}" id="i21"
validator="#{TreeSampleBean.ValidatorEmail}"/>



In ManagedBean, add the logic like this

    public void ValidatorEmail(FacesContext facesContext,
UIComponent uIComponent, Object object) {
FacesContext fc = FacesContext.getCurrentInstance();
String email = (String)object;
if (email != null && !email.contains("@")) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "The Email Id must contain @","The Email Id must contain @");
fc.addMessage(uIComponent.getClientId(fc),message);
((RichInputText)uIComponent).setValid(false);
}
}



This wont allow to submit the form unless user rectifies the validation error, because the UIComponent's state is invalid.

2 comments:

Anonymous said...

Thanks AMulya,
Your blog is one of very few valuable for a new ADF developer like me.
Keep blogging !

Yiannis

lava said...

thats superb!!!