dwinterfeldt 01/10/17 13:16:06
Modified: contrib/validator/src/share/com/wintecinc/struts/validation
StrutsValidator.java GenericValidator.java
Log:
Added min and max length validations.
Revision Changes Path
1.3 +78 -0
jakarta-struts/contrib/validator/src/share/com/wintecinc/struts/validation/StrutsValidator.java
Index: StrutsValidator.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/contrib/validator/src/share/com/wintecinc/struts/validation/StrutsValidator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- StrutsValidator.java 2001/09/25 18:00:11 1.2
+++ StrutsValidator.java 2001/10/17 20:16:06 1.3
@@ -457,4 +457,82 @@
return true;
}
}
+
+ /**
+ * <p>Checks if the field's length is less than or equal to the maximum value.
A <code>Null</code>
+ * will be considered an error.</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.
+ * @param application The application's <code>ServletContext</code>.
+ */
+ public static boolean validateMaxLength(Object bean,
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request,
ServletContext application) {
+
+ String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
+ String sMaxLength = field.getVarValue("maxlength");
+
+ // NullPointerException could occur, but it will be treated as failing the
validation
+ try {
+ int max = Integer.parseInt(sMaxLength);
+
+ if (!GenericValidator.maxLength(value, max)) {
+ errors.add(field.getKey(), ValidatorUtil.getActionError(application,
request, va, field));
+
+ return false;
+ }
+ } catch (Exception e) {
+ errors.add(field.getKey(), ValidatorUtil.getActionError(application,
request, va, field));
+ return false;
+ }
+
+ return true;
+ }
+
+
+ /**
+ * <p>Checks if the field's length is greater than or equal to the minimum
value.
+ * A <code>Null</code> will be considered an error.</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.
+ * @param application The application's <code>ServletContext</code>.
+ */
+ public static boolean validateMinLength(Object bean,
+ ValidatorAction va, Field field,
+ ActionErrors errors,
+ HttpServletRequest request,
ServletContext application) {
+
+ String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
+ String sMinLength = field.getVarValue("minlength");
+
+ // NullPointerException could occur, but it will be treated as failing the
validation
+ try {
+ int min = Integer.parseInt(sMinLength);
+
+ if (!GenericValidator.minLength(value, min)) {
+ errors.add(field.getKey(), ValidatorUtil.getActionError(application,
request, va, field));
+
+ return false;
+ }
+ } catch (Exception e) {
+ errors.add(field.getKey(), ValidatorUtil.getActionError(application,
request, va, field));
+ return false;
+ }
+
+ return true;
+ }
+
}
1.3 +27 -0
jakarta-struts/contrib/validator/src/share/com/wintecinc/struts/validation/GenericValidator.java
Index: GenericValidator.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/contrib/validator/src/share/com/wintecinc/struts/validation/GenericValidator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- GenericValidator.java 2001/09/13 16:08:37 1.2
+++ GenericValidator.java 2001/10/17 20:16:06 1.3
@@ -471,4 +471,31 @@
return bValid;
}
+
+ /**
+ * <p>Checks if the value's length is less than or equal to the max.</p>
+ *
+ * @param value The value validation is being performed on.
+ * @param max The maximum length.
+ */
+ public static boolean maxLength(String value, int max) {
+ if (value.length() <= max)
+ return true;
+ else
+ return false;
+ }
+
+ /**
+ * <p>Checks if the value's length is greater than or equal to the min.</p>
+ *
+ * @param value The value validation is being performed on.
+ * @param min The minimum length.
+ */
+ public static boolean minLength(String value, int min) {
+ if (value.length() >= min)
+ return true;
+ else
+ return false;
+ }
+
}