2
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/validator/DynaValidatorForm.java,v 1.16 2004/04/02 14:30:57 germuska Exp $
4
* $Date: 2004/04/02 14:30:57 $
6
* Copyright 2000-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.struts.validator;
23
import java.io.Serializable;
26
import javax.servlet.ServletContext;
27
import javax.servlet.http.HttpServletRequest;
29
import org.apache.commons.beanutils.DynaBean;
30
import org.apache.commons.logging.Log;
31
import org.apache.commons.logging.LogFactory;
32
import org.apache.commons.validator.Validator;
33
import org.apache.commons.validator.ValidatorException;
34
import org.apache.commons.validator.ValidatorResults;
35
import org.apache.struts.action.ActionErrors;
36
import org.apache.struts.action.ActionMapping;
37
import org.apache.struts.action.DynaActionForm;
40
* <p>This class extends <strong>DynaActionForm</strong> and provides
41
* basic field validation based on an XML file. The key passed into the
42
* validator is the action element's 'name' attribute from the
43
* struts-config.xml which should match the form element's name attribute
44
* in the validation.xml.</p>
46
* <ul><li>See <code>ValidatorPlugin</code> definition in struts-config.xml
47
* for validation rules.</li></ul>
49
* @version $Revision: 1.16 $ $Date: 2004/04/02 14:30:57 $
51
* @see org.apache.struts.action.ActionForm
53
public class DynaValidatorForm extends DynaActionForm implements DynaBean, Serializable {
56
* Commons Logging instance.
58
private static Log log = LogFactory.getLog(DynaValidatorForm.class);
61
* The results returned from the validation performed
62
* by the <code>Validator</code>.
64
protected ValidatorResults validatorResults = null;
67
* Used to indicate the current page of a multi-page form.
69
protected int page = 0;
73
* @return page number.
75
public int getPage() {
81
* @param page page number
83
public void setPage(int page) {
88
* Validate the properties that have been set from this HTTP request,
89
* and return an <code>ActionErrors</code> object that encapsulates any
90
* validation errors that have been found. If no errors are found, return
91
* <code>null</code> or an <code>ActionErrors</code> object with no
92
* recorded error messages.
94
* @param mapping The mapping used to select this instance.
95
* @param request The servlet request we are processing.
96
* @return <code>ActionErrors</code> object that encapsulates any validation errors.
98
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
99
this.setPageFromDynaProperty();
101
ServletContext application = getServlet().getServletContext();
102
ActionErrors errors = new ActionErrors();
104
String validationKey = getValidationKey(mapping, request);
106
Validator validator = Resources.initValidator(validationKey,
108
application, request,
112
validatorResults = validator.validate();
113
} catch (ValidatorException e) {
114
log.error(e.getMessage(), e);
121
* Returns the Validation key.
123
* @param mapping The mapping used to select this instance
124
* @param request The servlet request we are processing
125
* @return validation key - the form element's name in this case
127
public String getValidationKey(ActionMapping mapping,
128
HttpServletRequest request) {
130
return mapping.getAttribute();
134
* Sets this.page to the value of the Dyna property "page" if it's defined. This is
135
* used to setup the page variable before validation starts.
138
protected void setPageFromDynaProperty() {
139
Map props = this.getMap();
140
if (props.containsKey("page")) {
142
this.page = ((Integer) props.get("page")).intValue();
144
} catch (ClassCastException e) {
145
log.error("Dyna 'page' property must be of type java.lang.Integer.", e);
152
* Reset all properties to their default values.
154
* @param mapping The mapping used to select this instance
155
* @param request The servlet request we are processing
157
public void reset(ActionMapping mapping, HttpServletRequest request) {
158
super.reset(mapping, request);
160
validatorResults = null;
164
* Get results of the validation performed by the
165
* <code>Validator</code>.
166
* @return validator results as ValidatorResults object
168
public ValidatorResults getValidatorResults() {
169
return validatorResults;
173
* Set results of the validation performed by the
174
* <code>Validator</code>.
175
* @param validatorResults Set results of the validation performed
177
public void setValidatorResults(ValidatorResults validatorResults) {
178
this.validatorResults = validatorResults;
182
* Returns a <code>Map</code> of values returned
183
* from any validation that returns a value other than
184
* <code>null</code> or <code>Boolean</code> with the
185
* key the full property path of the field.
186
* @return Returns a <code>Map</code> of values, otherwise returns null if no results.
188
public Map getResultValueMap() {
189
return (validatorResults != null ? validatorResults.getResultValueMap() : null);