rleland 2002/10/16 11:44:30
Modified: conf/share validator-rules.xml
doc/userGuide dev_validator.xml
src/share/org/apache/struts/util StrutsValidator.java
Log:
Bug#:13528 Apply patch to allow validator to conditionally
require fields. supplied by James Turner [EMAIL PROTECTED]
Also turn his comments into userguide material.
Revision Changes Path
1.10 +15 -29 jakarta-struts/conf/share/validator-rules.xml
Index: validator-rules.xml
===================================================================
RCS file: /home/cvs/jakarta-struts/conf/share/validator-rules.xml,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- validator-rules.xml 11 Oct 2002 19:12:26 -0000 1.9
+++ validator-rules.xml 16 Oct 2002 18:44:30 -0000 1.10
@@ -81,6 +81,17 @@
</validator>
+ <validator name="requiredif"
+ classname="org.apache.struts.util.StrutsValidator"
+ method="validateRequiredIf"
+ methodParams="java.lang.Object,
+ org.apache.commons.validator.ValidatorAction,
+ org.apache.commons.validator.Field,
+ org.apache.struts.action.ActionErrors,
+ org.apache.commons.validator.Validator,
+ javax.servlet.http.HttpServletRequest"
+ msg="errors.required">
+ </validator>
<validator name="minlength"
classname="org.apache.struts.util.StrutsValidator"
@@ -586,10 +597,10 @@
</validator>
-
+<!-- range is deprecated use rangeInt instead -->
<validator name="range"
classname="org.apache.struts.util.StrutsValidator"
- method="validateRange"
+ method="validateIntRange"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
@@ -599,33 +610,8 @@
msg="errors.range">
<javascript><![CDATA[
- function validateRange(form) {
- var bValid = true;
- var focusField = null;
- var i = 0;
- var fields = new Array();
- oRange = new range();
- for (x in oRange) {
- if ((form[oRange[x][0]].type == 'text' ||
- form[oRange[x][0]].type == 'textarea') &&
- (form[oRange[x][0]].value.length > 0)) {
- var iMin = parseInt(oRange[x][2]("min"));
- var iMax = parseInt(oRange[x][2]("max"));
- var iValue = parseInt(form[oRange[x][0]].value);
- if (!(iValue >= iMin && iValue <= iMax)) {
- if (i == 0) {
- focusField = form[oRange[x][0]];
- }
- fields[i++] = oRange[x][1];
- bValid = false;
- }
- }
- }
- if (fields.length > 0) {
- focusField.focus();
- alert(fields.join('\n'));
- }
- return bValid;
+ function validateRange(form) {
+ return validIntRange(form);
}]]>
</javascript>
1.5 +99 -1 jakarta-struts/doc/userGuide/dev_validator.xml
Index: dev_validator.xml
===================================================================
RCS file: /home/cvs/jakarta-struts/doc/userGuide/dev_validator.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- dev_validator.xml 31 Aug 2002 13:15:02 -0000 1.4
+++ dev_validator.xml 16 Oct 2002 18:44:30 -0000 1.5
@@ -2,15 +2,113 @@
<document url="./resources.xml">
<properties>
<author>David Winterfeldt</author>
+ <author>James Turner</author>
+ <author>Rob Leland</author>
<title>The Struts User's Guide - Validator Guide</title>
</properties>
<body>
<chapter name="Struts Validator Guide">
<section href="validator" name="Struts Validator">
-<p>
+
[:TODO:]
+<p>Cover basic functionality as documented on David's web site.
+<a href="http://home.earthlink.net/~dwinterfeldt/">Validation Framework for Struts
</a>
</p>
+
+<p> Struts 1.1 has added additional functionality over the original validator
+contributed by David Winterfeldt</p>
+<ul>
+<li> Conditionally required fields</li>
+<li> intRange() & floatRange() methods in both JavaScript and Java</li>
+<li> deprecation of range() methods in both JavaScript and Java</li>
+</ul>
+
+<p>
+The most fundamental change is the ability to conditionally
+require validator fields based on the value of other fields.
+It allows you to define logic like "only validate this field if field X is non-
+null and field Y equals "male".</p>
+<p>
+The syntax looks like this:
+</p>
+<p>
+If you have this in your struts-config.xml </p>
+<ol>
+<pre> <form-bean name="dependentlistForm" </pre>
+<pre> type="org.apache.struts.webapp.validator.forms.ValidatorForm">
</pre>
+<pre> <form-property name="dependents" </pre>
+<pre> type="org.apache.struts.webapp.validator.Dependent[]"</pre>
+<pre> initial="{'','','','','','','','','','',''}"/></pre>
+<pre> <form-property name="insureDependents" type="java.lang.Boolean"
</pre>
+<pre> initial="false"/></pre>
+<pre> </form-bean></pre>
+</ol>
+<p>
+Where dependent is a bean that has properties lastName, firstName, dob,
+coverageType </p>
+<p>
+You can define a validation:</p>
+
+<pre> <form name="dependentlistForm"></pre>
+<pre> <field property="firstName" indexedListProperty="dependents" </pre>
+<pre> depends="requiredif"></pre>
+<pre> <arg0 key="dependentlistForm.firstName.label"/></pre>
+<pre> <var> <var-name>field[0]</var-name></pre>
+<pre> <var-value>lastName</var-value> </var></pre>
+<pre> <var> <var-name>field-indexed[0]</var-name></pre>
+<pre> <var-value>true</var-value>
</var></pre>
+<pre> <var> <var-name>field-test[0]</var-name></pre>
+<pre> <var-value>NOTNULL</var-value> </var></pre>
+<pre> </field></pre>
+<pre> <field property="dob" indexedListProperty="dependents" </pre>
+<pre> depends="requiredif,date"></pre>
+<pre> <arg0 key="dependentlistForm.dob.label"/></pre>
+<pre> <var> <var-name>field[0]</var-name> </pre>
+<pre> <var-value>lastName</var-value> </var></pre>
+<pre> <var> <var-name>field-indexed[0]</var-name> </pre>
+<pre> <var-value>true</var-value>
</var></pre>
+<pre> <var> <var-name>field-test[0]</var-name> </pre>
+<pre> <var-value>NOTNULL</var-value></var></pre>
+<pre> </field></pre>
+
+<pre> <field property="coverageType" indexedListProperty="dependents"</pre>
+<pre> depends="requiredif"></pre>
+<pre> <arg0 key="dependentlistForm.coverageType.label"/></pre>
+<pre> <var> <var-name>field[0]</var-name></pre>
+<pre> <var-value>lastName</var-value> </var></pre>
+<pre> <var> <var-name>field-indexed[0]</var-name></pre>
+<pre> <var-value>true</var-value>
</var></pre>
+<pre> <var> <var-name>field-test[0]</var-name></pre>
+<pre> <var-value>NOTNULL</var-value> </var></pre>
+<pre> <var> <var-name>field[1]</var-name></pre>
+<pre> <var-value>insureDependents</var-value>
</var></pre>
+<pre> <var> <var-name>field-test[1]</var-name> </pre>
+<pre> <var-value>EQUAL</var-value> </var></pre>
+<pre> <var> <var-name>field-value[1]</var-name></pre>
+<pre> <var-value>true</var-value>
</var></pre>
+<pre> <var> <var-name>field-join</var-name> </pre>
+<pre> <var-value>AND</var-value>
</var></pre>
+<pre> </field></pre>
+<pre> </form></pre>
+<p>
+Which is read as follows:
+The firstName field is only required if the lastName field is non-null. Since
+field-indexed is true, it means that lastName must be a property of the same
+indexed field as firstName. Same thing for dob, except that we validate for
+date if not blank.
+</p>
+<p>
+The coverageType is only required if the lastName for the same indexed bean is
+not null, and also if the non-indexed field insureDependents is true.
+</p>
+<p>
+You can have an arbitrary number of fields by using the [n] syntax, the only
+restriction is that they must all be AND or OR, you can't mix.
+</p>
+
+
+
</section>
1.11 +291 -195
jakarta-struts/src/share/org/apache/struts/util/StrutsValidator.java
Index: StrutsValidator.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/share/org/apache/struts/util/StrutsValidator.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- StrutsValidator.java 14 Oct 2002 16:35:25 -0000 1.10
+++ StrutsValidator.java 16 Oct 2002 18:44:30 -0000 1.11
@@ -58,6 +58,7 @@
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.validator.Field;
@@ -65,6 +66,7 @@
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorUtil;
+import org.apache.commons.validator.Validator;
import org.apache.struts.action.ActionErrors;
/**
@@ -89,6 +91,10 @@
*/
private static Log LOG = LogFactory.getLog(StrutsValidator.class);
+ public final static String FIELD_TEST_NULL = "NULL";
+ public final static String FIELD_TEST_NOTNULL = "NOTNULL";
+ public final static String FIELD_TEST_EQUAL = "EQUAL";
+
/**
* <p>
@@ -106,9 +112,9 @@
*@return True if meets stated requirements, False otherwise
*/
public static boolean validateRequired(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
String value = null;
if (isString(bean)) {
@@ -118,7 +124,7 @@
}
if (GenericValidator.isBlankOrNull(value)) {
errors.add(field.getKey(),
- StrutsValidatorUtil.getActionError(request, va, field));
+ StrutsValidatorUtil.getActionError(request, va,
field));
return false;
} else {
@@ -131,6 +137,96 @@
/**
* <p>
*
+ * Checks if the field isn't null based on the values of other fields
+ * </p>
+ *
+ *@param bean The bean validation is being performed on.
+ *@param va The <code>ValidatorAction</code> that is currently being
performed.
+ *@param field The <code>Field</code> object associated with the current
+ * field being validated.
+ *@param errors The <code>ActionErrors</code> object to add errors to if any
+ * validation errors occur.
+ *@param validator The <code>Validator</code> instance, used to access other
field values.
+ *@param request Current request object.
+ *@return True if meets stated requirements, False otherwise
+ */
+ public static boolean validateRequiredIf(Object bean,
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ Validator validator,
+ HttpServletRequest request) {
+ Object form = validator.getResource(Validator.BEAN_KEY);
+ String value = null;
+ boolean required = false;
+ if (isString(bean)) {
+ value = (String) bean;
+ } else {
+ value = ValidatorUtil.getValueAsString(bean, field.getProperty());
+ }
+ int i = 0;
+ String fieldJoin = "AND";
+ if (!GenericValidator.isBlankOrNull(field.getVarValue("field-join"))) {
+ fieldJoin = field.getVarValue("field-join");
+ }
+ if (fieldJoin.equalsIgnoreCase("AND")) {
+ required = true;
+ }
+ while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i +
"]"))) {
+ String dependProp = field.getVarValue("field[" + i + "]");
+ String dependTest = field.getVarValue("field-test[" + i + "]");
+ String dependTestValue = field.getVarValue("field-value[" + i + "]");
+ String dependIndexed = field.getVarValue("field-indexed[" + i + "]");
+ if (dependIndexed == null)
+ dependIndexed = "false";
+ String dependVal = null;
+ boolean this_required = false;
+ if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
+ String key = field.getKey();
+ if ((key.indexOf("[") > -1) &&
+ (key.indexOf("]") > -1)) {
+ String ind = key.substring(0, key.indexOf(".") + 1);
+ dependProp = ind + dependProp;
+ }
+ }
+ dependVal = ValidatorUtil.getValueAsString(form, dependProp);
+ if (dependTest.equals(FIELD_TEST_NULL)) {
+ if ((dependVal != null) && (dependVal.length() > 0)) {
+ this_required = false;
+ } else {
+ this_required = true;
+ }
+ }
+ if (dependTest.equals(FIELD_TEST_NOTNULL)) {
+ if ((dependVal != null) && (dependVal.length() > 0)) {
+ this_required = true;
+ } else {
+ this_required = false;
+ }
+ }
+ if (dependTest.equals(FIELD_TEST_EQUAL)) {
+ this_required = dependTestValue.equalsIgnoreCase(dependVal);
+ }
+ if (fieldJoin.equalsIgnoreCase("AND")) {
+ required = required && this_required;
+ } else {
+ required = required || this_required;
+ }
+ i++;
+ }
+ if (required) {
+ if ((value != null) && (value.length() > 0)) {
+ return true;
+ } else {
+ errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * <p>
+ *
* Checks if the field matches the regular expression in the field's mask
attribute.
* </p>
*
@@ -144,9 +240,9 @@
*@return True if field matches mask, false otherwise.
*/
public static boolean validateMask(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
String mask = field.getVarValue("mask");
String value = null;
@@ -154,14 +250,14 @@
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean,
- field.getProperty());
+ field.getProperty());
}
try {
if (!GenericValidator.isBlankOrNull(value) &&
- !GenericValidator.matchRegexp(value, mask)) {
+ !GenericValidator.matchRegexp(value, mask)) {
errors.add(field.getKey(),
- StrutsValidatorUtil.getActionError(request, va,
- field));
+ StrutsValidatorUtil.getActionError(request, va,
+ field));
return false;
} else {
@@ -189,9 +285,9 @@
*@return A Byte if valid, a null otherwise.
*/
public static Byte validateByte(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
Byte result = null;
String value = null;
@@ -228,9 +324,9 @@
*@return A Short if valid, otherwise a null.
*/
public static Short validateShort(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
Short result = null;
String value = null;
if (isString(bean)) {
@@ -266,9 +362,9 @@
*@return An Integer if valid, a null otherwise.
*/
public static Integer validateInteger(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
Integer result = null;
String value = null;
if (isString(bean)) {
@@ -304,9 +400,9 @@
*@return A Long if valid, a null otherwise.
*/
public static Long validateLong(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
Long result = null;
String value = null;
if (isString(bean)) {
@@ -342,9 +438,9 @@
*@return A Float if valid, a null otherwise.
*/
public static Float validateFloat(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
Float result = null;
String value = null;
if (isString(bean)) {
@@ -380,9 +476,9 @@
*@return A Double if valid, a null otherwise.
*/
public static Double validateDouble(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
Double result = null;
String value = null;
if (isString(bean)) {
@@ -425,9 +521,9 @@
*@return A Date if valid, a null if blank or invalid.
*/
public static Date validateDate(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
Date result = null;
String value = null;
@@ -477,158 +573,158 @@
*@return True if in range, false otherwise.
*/
public static boolean validateRange(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
- return validateIntRange(bean,va,field,errors,request);
- }
-
- /**
- * <p>
- *
- * Checks if a fields value is within a range (min & max specified in the
- * vars attribute).</p>
- *
- *@param bean The bean validation is being performed on.
- *@param va The <code>ValidatorAction</code> that is currently being
performed.
- *@param field The <code>Field</code> object associated with the current
- * field being validated.
- *@param errors The <code>ActionErrors</code> object to add errors to if any
- * validation errors occur.
- *@param request Current request object.
- *@return True if in range, false otherwise.
- */
- public static boolean validateIntRange(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
-
- String value = null;
- if (isString(bean)) {
- value = (String) bean;
- } else {
- value = ValidatorUtil.getValueAsString(bean, field.getProperty());
- }
- String sMin = field.getVarValue("min");
- String sMax = field.getVarValue("max");
-
- if (!GenericValidator.isBlankOrNull(value)) {
- try {
- int iValue = Integer.parseInt(value);
- int min = Integer.parseInt(sMin);
- int max = Integer.parseInt(sMax);
-
- if (!GenericValidator.isInRange(iValue, min, max)) {
- errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
-
- return false;
- }
- } catch (Exception e) {
- errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * <p>
- *
- * Checks if a fields value is within a range (min & max specified in the
- * vars attribute).</p>
- *
- *@param bean The bean validation is being performed on.
- *@param va The <code>ValidatorAction</code> that is currently being
performed.
- *@param field The <code>Field</code> object associated with the current
- * field being validated.
- *@param errors The <code>ActionErrors</code> object to add errors to if any
- * validation errors occur.
- *@param request Current request object.
- *@return True if in range, false otherwise.
- */
- public static boolean validateDoubleRange(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
-
- String value = null;
- if (isString(bean)) {
- value = (String) bean;
- } else {
- value = ValidatorUtil.getValueAsString(bean, field.getProperty());
- }
- String sMin = field.getVarValue("min");
- String sMax = field.getVarValue("max");
-
- if (!GenericValidator.isBlankOrNull(value)) {
- try {
- double dValue = Double.parseDouble(value);
- double min = Double.parseDouble(sMin);
- double max = Double.parseDouble(sMax);
-
- if (!GenericValidator.isInRange(dValue, min, max)) {
- errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
-
- return false;
- }
- } catch (Exception e) {
- errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * <p>
- *
- * Checks if a fields value is within a range (min & max specified in the
- * vars attribute).</p>
- *
- *@param bean The bean validation is being performed on.
- *@param va The <code>ValidatorAction</code> that is currently being
performed.
- *@param field The <code>Field</code> object associated with the current
- * field being validated.
- *@param errors The <code>ActionErrors</code> object to add errors to if any
- * validation errors occur.
- *@param request Current request object.
- *@return True if in range, false otherwise.
- */
- public static boolean validateFloatRange(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
-
- String value = null;
- if (isString(bean)) {
- value = (String) bean;
- } else {
- value = ValidatorUtil.getValueAsString(bean, field.getProperty());
- }
- String sMin = field.getVarValue("min");
- String sMax = field.getVarValue("max");
-
- if (!GenericValidator.isBlankOrNull(value)) {
- try {
- float fValue = Float.parseFloat(value);
- float min = Float.parseFloat(sMin);
- float max = Float.parseFloat(sMax);
-
- if (!GenericValidator.isInRange(fValue, min, max)) {
- errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
-
- return false;
- }
- } catch (Exception e) {
- errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
- return false;
- }
- }
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
+ return validateIntRange(bean, va, field, errors, request);
+ }
+
+ /**
+ * <p>
+ *
+ * Checks if a fields value is within a range (min & max specified in the
+ * vars attribute).</p>
+ *
+ *@param bean The bean validation is being performed on.
+ *@param va The <code>ValidatorAction</code> that is currently being
performed.
+ *@param field The <code>Field</code> object associated with the current
+ * field being validated.
+ *@param errors The <code>ActionErrors</code> object to add errors to if any
+ * validation errors occur.
+ *@param request Current request object.
+ *@return True if in range, false otherwise.
+ */
+ public static boolean validateIntRange(Object bean,
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
+
+ String value = null;
+ if (isString(bean)) {
+ value = (String) bean;
+ } else {
+ value = ValidatorUtil.getValueAsString(bean, field.getProperty());
+ }
+ String sMin = field.getVarValue("min");
+ String sMax = field.getVarValue("max");
+
+ if (!GenericValidator.isBlankOrNull(value)) {
+ try {
+ int iValue = Integer.parseInt(value);
+ int min = Integer.parseInt(sMin);
+ int max = Integer.parseInt(sMax);
+
+ if (!GenericValidator.isInRange(iValue, min, max)) {
+ errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
+
+ return false;
+ }
+ } catch (Exception e) {
+ errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
+ return false;
+ }
+ }
- return true;
- }
+ return true;
+ }
+
+ /**
+ * <p>
+ *
+ * Checks if a fields value is within a range (min & max specified in the
+ * vars attribute).</p>
+ *
+ *@param bean The bean validation is being performed on.
+ *@param va The <code>ValidatorAction</code> that is currently being
performed.
+ *@param field The <code>Field</code> object associated with the current
+ * field being validated.
+ *@param errors The <code>ActionErrors</code> object to add errors to if any
+ * validation errors occur.
+ *@param request Current request object.
+ *@return True if in range, false otherwise.
+ */
+ public static boolean validateDoubleRange(Object bean,
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
+
+ String value = null;
+ if (isString(bean)) {
+ value = (String) bean;
+ } else {
+ value = ValidatorUtil.getValueAsString(bean, field.getProperty());
+ }
+ String sMin = field.getVarValue("min");
+ String sMax = field.getVarValue("max");
+
+ if (!GenericValidator.isBlankOrNull(value)) {
+ try {
+ double dValue = Double.parseDouble(value);
+ double min = Double.parseDouble(sMin);
+ double max = Double.parseDouble(sMax);
+
+ if (!GenericValidator.isInRange(dValue, min, max)) {
+ errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
+
+ return false;
+ }
+ } catch (Exception e) {
+ errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * <p>
+ *
+ * Checks if a fields value is within a range (min & max specified in the
+ * vars attribute).</p>
+ *
+ *@param bean The bean validation is being performed on.
+ *@param va The <code>ValidatorAction</code> that is currently being
performed.
+ *@param field The <code>Field</code> object associated with the current
+ * field being validated.
+ *@param errors The <code>ActionErrors</code> object to add errors to if any
+ * validation errors occur.
+ *@param request Current request object.
+ *@return True if in range, false otherwise.
+ */
+ public static boolean validateFloatRange(Object bean,
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
+
+ String value = null;
+ if (isString(bean)) {
+ value = (String) bean;
+ } else {
+ value = ValidatorUtil.getValueAsString(bean, field.getProperty());
+ }
+ String sMin = field.getVarValue("min");
+ String sMax = field.getVarValue("max");
+
+ if (!GenericValidator.isBlankOrNull(value)) {
+ try {
+ float fValue = Float.parseFloat(value);
+ float min = Float.parseFloat(sMin);
+ float max = Float.parseFloat(sMax);
+
+ if (!GenericValidator.isInRange(fValue, min, max)) {
+ errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
+
+ return false;
+ }
+ } catch (Exception e) {
+ errors.add(field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field));
+ return false;
+ }
+ }
+
+ return true;
+ }
/**
@@ -651,9 +747,9 @@
*@return The credit card as a Long, a null if invalid, blank, or null.
*/
public static Long validateCreditCard(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
Long result = null;
String value = null;
@@ -693,9 +789,9 @@
*@return True if valid, false otherwise.
*/
public static boolean validateEmail(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
String value = null;
if (isString(bean)) {
@@ -729,9 +825,9 @@
*@return True if stated conditions met.
*/
public static boolean validateMaxLength(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
String value = null;
if (isString(bean)) {
@@ -776,9 +872,9 @@
*@return True if stated conditions met.
*/
public static boolean validateMinLength(Object bean,
- ValidatorAction va, Field field,
- ActionErrors errors,
- HttpServletRequest request) {
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request) {
String value = null;
if (isString(bean)) {
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>