KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > core > validator > EmptyFieldValidator


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.faces.core.validator;
6
7 import javax.faces.component.UIComponent;
8 import javax.faces.context.FacesContext;
9 import javax.faces.validator.Validator;
10 import javax.faces.validator.ValidatorException;
11 import org.exoplatform.faces.application.ExoFacesMessage;
12 import org.exoplatform.faces.core.component.UIInput;
13 import org.exoplatform.faces.core.component.UIForm;
14 /**
15  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
16  * @since Aug 13, 2004
17  * @version $Id: EmptyStringValidator.java,v 1.4 2004/08/19 14:53:44 tuan08 Exp $
18  */

19 public class EmptyFieldValidator implements Validator {
20   public void validate(FacesContext context,
21                        UIComponent component,
22                        Object JavaDoc value) throws ValidatorException {
23     if(component instanceof UIForm) {
24       if(value instanceof UIForm.IntegerField) {
25       } else if(value instanceof UIForm.StringField) {
26         UIForm.StringField field = (UIForm.StringField) value ;
27         String JavaDoc s = field.getValue() ;
28         if(s == null || s.length() == 0) {
29           Object JavaDoc[] args = {field.getName()} ;
30           field.setError(true) ;
31           throw new ValidatorException(new ExoFacesMessage("#{EmptyStringValidator.msg.empty-input}", args)) ;
32         }
33       }
34     } else {
35       String JavaDoc s = (String JavaDoc) value ;
36       if(s == null || s.length() == 0) {
37         UIInput uiInput = (UIInput) component ;
38         Object JavaDoc[] args = {uiInput.getName()} ;
39         throw new ValidatorException(new ExoFacesMessage("#{EmptyStringValidator.msg.empty-input}", args)) ;
40       }
41     }
42   }
43 }
Popular Tags