I'm trying to use the validator with a regexp mask on an textarea field. There are two problems:It turns out that the mask validator is not able to validate String[] objects at all, so it cannot validate textareas. I wrote my own validateMask method:
1) even if the regexp allows empty strings and the field is not required, the validator reports that the field is invalid. same mask works for a simple text input. 2) I'm trying to use the regexp on every line of the textarea, ie: Alternate EMail Addresses [EMAIL PROTECTED] [EMAIL PROTECTED] Every line should evaluated by the email target of the validator. But this doesn't work. Even empty string don't work.
/** * Checks if the field matches the regular expression in the field's mask 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>ActionErrors</code> object to add errors to if * any validation errors occur. * @param request Current request object. * @return true if field matches mask, false otherwise. */ public static boolean validateMask(Object bean, ValidatorAction va, Field field, ActionErrors errors, HttpServletRequest request) {
String mask = field.getVarValue("mask"); String value = null; Object object = null;
if (isString(bean)) { value = (String) bean; } else {
try { object = PropertyUtils.getProperty(bean, field.getProperty()); } catch (Exception e) { log.error(e.getMessage(), e); }
// check if we have a string array if (object instanceof String[]) { String[] strarray = ((String[])object)[0].split("\\r\\n"); boolean valid = true; int line;
// iterate over the array and apply the mask to ever string // exit on first mismatch for (line = 0; line < strarray.length; line++) { if (!GenericValidator.isBlankOrNull(strarray[line]) && !GenericValidator.matchRegexp(strarray[line], mask)) { valid=false; break; } }
// get the key of the error message, and create a new action error with it. // add the failed line number as parameter if (!valid) { String arg[] = Resources.getArgs(va.getName(), Resources.getMessageResources(request), Resources.getLocale(request), field); String msg = (field.getMsg(va.getName()) != null ? field.getMsg(va.getName()) : va.getMsg()); errors.add(field.getKey(), new ActionError(msg, arg[0], String.valueOf(line + 1)));
return false; }
return true; }
// no string array, we're simply going to convert the object to string value = object.toString(); } if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.matchRegexp(value, mask)) {
errors.add(field.getKey(), Resources.getActionError(request, va, field)); return false; }
return true; }
It displays the number of the line that failed the validation in the error message. This is the entry of my validator-rules.xml
<validator name="arraymask"
classname="de.xylon.ldapgw.validator.FieldChecks"
method="validateMask"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.array.invalid"/>
and the appropriate error message:
> errors.array.invalid={0} is invalid on line {1}.
Regards, Arne
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]

