View Javadoc

1   /*
2    * $Id: RequiredFieldValidator.java,v 1.6 2004/04/25 10:02:35 eelco12 Exp $
3    * $Revision: 1.6 $
4    * $Date: 2004/04/25 10:02:35 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package nl.openedge.baritus.validation.impl;
32  
33  import nl.openedge.baritus.FormBeanContext;
34  import nl.openedge.baritus.validation.AbstractFieldValidator;
35  import nl.openedge.baritus.validation.ValidationActivationRule;
36  
37  import org.infohazard.maverick.flow.ControllerContext;
38  
39  /***
40   * Checks for a non-EMPTY input.
41   * Use this for fields that should have a not null (empty string) input.
42   * Note that as this validator is a field validator, and thus is registered
43   * for a single field, it is only fired if a field (e.g. a request parameter)
44   * is actually provided. In other words: if an instance of a required field
45   * validator was registered for field name 'myprop', but 'myprop' is not part
46   * of the request parameters, this validator never fires. Hence, if you want
47   * to be REALLY sure that a property of the form is not null, use a form validator
48   * (PropertyNotNullValidator). RequiredFieldValidator works fine for most cases
49   * where you have a HTML form with a field that should have a non empty value, but
50   * that - if a user fools around - does not seriousely break anything when a value
51   * is not provided (e.g. you probably have not null constraint in you database as well).
52   * 
53   * @author Eelco Hillenius
54   */
55  public class RequiredFieldValidator extends AbstractFieldValidator
56  {
57  
58  	private String errorMessageKey = "input.field.required";
59  
60  	/***
61  	 * Construct using 'input.field.required' as the message prefix.
62  	 */
63  	public RequiredFieldValidator()
64  	{
65  
66  	}
67  
68  	/***
69  	 * Construct using the provided activation rule and 'input.field.required' as the message prefix.
70  	 * @param rule
71  	 */
72  	public RequiredFieldValidator(ValidationActivationRule rule)
73  	{
74  		setValidationRule(rule);
75  	}
76  
77  	/***
78  	 * Construct using errorMessageKey and the activation rule
79  	 * @param errorMessageKey
80  	 * @param rule
81  	 */
82  	public RequiredFieldValidator(
83  		String errorMessageKey,
84  		ValidationActivationRule rule)
85  	{
86  		setErrorMessageKey(errorMessageKey);
87  		setValidationRule(rule);
88  	}
89  
90  	/***
91  	 * Construct with errorMessageKey for error message keys.
92  	 * @param errorMessageKey errorMessageKey
93  	 */
94  	public RequiredFieldValidator(String errorMessageKey)
95  	{
96  		setErrorMessageKey(errorMessageKey);
97  	}
98  
99  	/***
100 	 * Checks whether the value is not null, and - if it is an instance of String - whether
101 	 * the trimmed value is not an empty string.
102 	 * Note that as this validator is a field validator, and thus is registered
103 	 * for a single field, it is only fired if a field (e.g. a request parameter)
104 	 * is actually provided. In other words: if an instance of a required field
105 	 * validator was registered for field name 'myprop', but 'myprop' is not part
106 	 * of the request parameters, this validator never fires. Hence, if you want
107 	 * to be REALLY sure that a property of the form is not null, use a form validator
108 	 * (PropertyNotNullValidator). RequiredFieldValidator works fine for most cases
109 	 * where you have a HTML form with a field that should have a non empty value, but
110 	 * that - if a user fools around - does not seriousely break anything when a value
111 	 * is not provided (e.g. you probably have not null constraint in you database as well).
112 	 * 
113 	 * @return boolean true if not null or empty, false otherwise
114 	 * 
115 	 * @see nl.openedge.baritus.validation.FieldValidator#isValid(org.infohazard.maverick.flow.ControllerContext, nl.openedge.baritus.FormBeanContext, java.lang.String, java.lang.Object)
116 	 */
117 	public boolean isValid(
118 		ControllerContext cctx,
119 		FormBeanContext formBeanContext,
120 		String fieldName,
121 		Object value)
122 	{
123 		boolean isValid = true;
124 		if(value == null)
125 		{
126 			isValid = false;
127 		}
128 		else
129 		{
130 			if(value instanceof String)
131 			{
132 				if("".equals(((String)value).trim()))
133 				{
134 					isValid = false;
135 				}
136 			}
137 		}
138 		
139 		if(!isValid)
140 		{
141 			setErrorMessage(formBeanContext, fieldName, getErrorMessageKey(), 
142 				new Object[]{getFieldName(formBeanContext, fieldName)});
143 		}
144 		
145 		return isValid;
146 	}
147 	
148 	/***
149 	 * Get key of error message.
150 	 * @return String key of error message
151 	 */
152 	public String getErrorMessageKey()
153 	{
154 		return errorMessageKey;
155 	}
156 
157 	/***
158 	 * Set key of error message.
159 	 * @param string key of error message
160 	 */
161 	public void setErrorMessageKey(String string)
162 	{
163 		errorMessageKey = string;
164 	}
165 
166 }