Re: Textarea validation, using validator and mask

2003-12-01 Thread Arne Brutschy
I wrote:
I'm trying to use the validator with a regexp mask on an textarea field. 
 There are two problems:

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.
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:

  /**
   * 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 ValidatorAction that is currently being
   * performed.
   * @param field The Field object associated with the current
   * field being validated.
   * @param errors   The ActionErrors 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

 
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]


Textarea validation, using validator and mask

2003-12-01 Thread Arne Brutschy
Hello,

I'm trying to use the validator with a regexp mask on an textarea field. 
 There are two problems:

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.
Anyone noticed this/has a solution? I'm going to try to understand this 
javascipt code in the mask target now, perhaps I can fix it.

Regards,
Arne
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]