Topic
1 reply Latest Post - ‏2012-05-10T16:20:57Z by gloriay
PeterPenzov
PeterPenzov
1 Post
ACCEPTED ANSWER

Pinned topic How to create JSF input validator

‏2012-05-09T20:34:21Z |
Hi,
I want to implement input field validator in JSF form:



//JSF page <h:panelGroup>Session ID</h:panelGroup> <h:panelGroup> <h:inputText id=
"sessionid" value=
"#{DatabaseController.formMap['sessionid']}" > <f:validateLength minimum=
"0" maximum=
"15"/> <f:validator binding=
"#{ValidatorController.validateMessage}" > <f:attribute name=
"type" value=
"sessionid" /> </f:validator> <f:ajax event=
"blur" render=
"sessionid" /> </h:inputText> <h:message id=
"sessionidMessage" for=
"sessionid" /> </h:panelGroup> 
// validator 

package com.DX_57.HM_57; 

import java.io.Serializable; 

import javax.enterprise.context.SessionScoped; 

import javax.faces.application.FacesMessage; 

import javax.faces.component.UIComponent; 

import javax.faces.context.FacesContext; 

import javax.faces.validator.Validator; 

import javax.faces.validator.ValidatorException; 

import javax.inject.Named; @Named(
"ValidatorController") @SessionScoped 

public 

class FormValidator 

implements Validator, Serializable 
{ 

public FormValidator() 
{ 
} @Override 

public 

void validate(FacesContext context, UIComponent component, Object value) 

throws ValidatorException 
{ 

throw 

new UnsupportedOperationException(
"Not supported yet."); 
} 

public 

void validateMessage(FacesContext context, UIComponent component, Object value) 

throws ValidatorException 
{ 

if (context.equals(
"test")) 
{ 

throw 

new ValidatorException(

new FacesMessage(FacesMessage.SEVERITY_ERROR, 
"Session ID is already in use, please choose another.", 

null)); 
} 
} 

public 

void validateUsername(FacesContext context, UIComponent component, Object value) 

throws ValidatorException 
{ 

if (context.equals(
"test")) 
{ 

throw 

new ValidatorException(

new FacesMessage(FacesMessage.SEVERITY_ERROR, 
"Username is already in use, please choose another.", 

null)); 
} 
} 
}


I tested the above code - it's not working. Nothing happens.

I have a JSF form with 20 input fields. I have to create validators for them. As for as I understand I have to create for each one of them separate validator into separate Java class. This means that I need to add 20 .java files into my WAR package. Is it possible to reduce the number of the .java files and use one big java validator for all input fields. Something like the above code?

If you want to implement for example 20 validators how would you implement the code in order to simplify it and reduce the number of the java classes?

Best Wishes
Peter
Updated on 2012-05-10T16:20:57Z at 2012-05-10T16:20:57Z by gloriay
  • gloriay
    gloriay
    5 Posts
    ACCEPTED ANSWER

    Re: How to create JSF input validator

    ‏2012-05-10T16:20:57Z  in response to PeterPenzov
    Hi,

    Try with the info in this link http://www.ibm.com/developerworks/java/library/j-jsf3/, there you can find information about JSF Converters and Validators, even is for JSP should works for Facelets