You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by mr...@apache.org on 2005/06/19 18:45:51 UTC
svn commit: r191349 - in /struts/core/trunk: conf/share/validator-rules.xml
doc/userGuide/dev_validator.xml
src/share/org/apache/struts/validator/FieldChecks.java
Author: mrdon
Date: Sun Jun 19 09:45:49 2005
New Revision: 191349
URL: http://svn.apache.org/viewcvs?rev=191349&view=rev
Log:
Adding longRange validation
PR: 17281
Modified:
struts/core/trunk/conf/share/validator-rules.xml
struts/core/trunk/doc/userGuide/dev_validator.xml
struts/core/trunk/src/share/org/apache/struts/validator/FieldChecks.java
Modified: struts/core/trunk/conf/share/validator-rules.xml
URL: http://svn.apache.org/viewcvs/struts/core/trunk/conf/share/validator-rules.xml?rev=191349&r1=191348&r2=191349&view=diff
==============================================================================
--- struts/core/trunk/conf/share/validator-rules.xml (original)
+++ struts/core/trunk/conf/share/validator-rules.xml Sun Jun 19 09:45:49 2005
@@ -233,6 +233,18 @@
depends="integer"
msg="errors.range"/>
+ <validator name="longRange"
+ classname="org.apache.struts.validator.FieldChecks"
+ method="validateLongRange"
+ methodParams="java.lang.Object,
+ org.apache.commons.validator.ValidatorAction,
+ org.apache.commons.validator.Field,
+ org.apache.struts.action.ActionMessages,
+ org.apache.commons.validator.Validator,
+ javax.servlet.http.HttpServletRequest"
+ depends="long"
+ msg="errors.range"/>
+
<validator name="floatRange"
classname="org.apache.struts.validator.FieldChecks"
Modified: struts/core/trunk/doc/userGuide/dev_validator.xml
URL: http://svn.apache.org/viewcvs/struts/core/trunk/doc/userGuide/dev_validator.xml?rev=191349&r1=191348&r2=191349&view=diff
==============================================================================
--- struts/core/trunk/doc/userGuide/dev_validator.xml (original)
+++ struts/core/trunk/doc/userGuide/dev_validator.xml Sun Jun 19 09:45:49 2005
@@ -266,7 +266,7 @@
]]></code></pre>
</li>
<li><strong>range</strong> - validate number range.
- <pre>Deprecated, use intRange, floatRange or doubleRange.</pre>
+ <pre>Deprecated, use intRange, longRange, floatRange or doubleRange.</pre>
</li>
<li><strong>intRange</strong> - validates that an integer field is within a specified range.
Requires <code>min</code> and <code>max</code> variables to specify the range.
@@ -277,6 +277,20 @@
<arg0 key="employee.age"/>
<arg1 name="intRange" key="${var:min}" resource="false"/>
<arg2 name="intRange" key="${var:max}" resource="false"/>
+ <var><var-name>min</var-name><var-value>18</var-value></var>
+ <var><var-name>max</var-name><var-value>65</var-value></var>
+ </field>
+ ]]></code></pre>
+ </li>
+ <li><strong>longRange</strong> - validates that an long field is within a specified range.
+ Requires <code>min</code> and <code>max</code> variables to specify the range.
+ This validator depends on the <code>long</code> validator
+ which must also be in the field's <code>depends</code> attribute.
+ <pre><code><![CDATA[
+ <field property="age" depends="required,long,longRange">
+ <arg0 key="employee.age"/>
+ <arg1 name="longRange" key="${var:min}" resource="false"/>
+ <arg2 name="longRange" key="${var:max}" resource="false"/>
<var><var-name>min</var-name><var-value>18</var-value></var>
<var><var-name>max</var-name><var-value>65</var-value></var>
</field>
Modified: struts/core/trunk/src/share/org/apache/struts/validator/FieldChecks.java
URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/validator/FieldChecks.java?rev=191349&r1=191348&r2=191349&view=diff
==============================================================================
--- struts/core/trunk/src/share/org/apache/struts/validator/FieldChecks.java (original)
+++ struts/core/trunk/src/share/org/apache/struts/validator/FieldChecks.java Sun Jun 19 09:45:49 2005
@@ -534,6 +534,52 @@
return result == null ? Boolean.FALSE : result;
}
+
+ /**
+ * Checks if a fields value is within a range (min & max specified in the
+ * vars attribute).
+ *
+ * @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>ActionMessages</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 in range, false otherwise.
+ */
+ public static boolean validateLongRange(Object bean,
+ ValidatorAction va, Field field,
+ ActionMessages errors,
+ Validator validator,
+ HttpServletRequest request) {
+
+ String value = null;
+ value = evaluateBean(bean, field);
+
+ if (!GenericValidator.isBlankOrNull(value)) {
+ try {
+ long longValue = Long.parseLong(value);
+ long min = Long.parseLong(field.getVarValue("min"));
+ long max = Long.parseLong(field.getVarValue("max"));
+
+ if (!GenericValidator.isInRange(longValue, min, max)) {
+ errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
+
+ return false;
+ }
+ } catch (Exception e) {
+ errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+
/**
* Checks if a fields value is within a range (min & max specified in the
* vars attribute).
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org