Struts validator won't work

E

Edwinek

Hi,

I've made a validator for comparing one form field with another and
return true for equality. However, when I test the code, I get this
stack trace:

[17/11/04 14:43:31:203 CET] 5f57da12 ValidatorForm E
org.apache.struts.validator.ValidatorForm TRAS0014I: The following
exception was logged org.apache.commons.validator.ValidatorException:
validateEqualToField
at org.apache.commons.validator.Field.validateForRule(Field.java:811)
at org.apache.commons.validator.Field.validate(Field.java:901)
at org.apache.commons.validator.Form.validate(Form.java:178)
at org.apache.commons.validator.Validator.validate(Validator.java:373)
at org.apache.struts.validator.ValidatorForm.validate(ValidatorForm.java:117)
... etc ...

The actual validator method is never reached (I put a breakpoint there
to make sure). Any ideas? This is the code:

<form name="formbeaninterhelpupdatemembersecurity">
<field property="pinCode" depends="regexp, comparewithotherfield">
<msg name="regexp"
key="interhelp.error.field.number.length.fixed"/>
<arg0 name="regexp" key="interhelp.updatemembersecurity.pincode"/>
<arg1 name="regexp" key="${var:maxlength}" resource="false"/>
<msg name="comparewithotherfield"
key="interhelp.error.field.compare"/>
<arg0 name="comparewithotherfield"
key="interhelp.updatemembersecurity.pincode"/>
<var>
<var-name>regexp</var-name>
<var-value>[0-9][0-9][0-9]</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>3</var-value>
</var>
<var>
<var-name>comparewithotherfield</var-name>
<var-value>pinCodeCheck</var-value>
</var>
</field>
<field property="answer" depends="required, maxlength">
<msg name="required" key="interhelp.error.field.required"/>
<arg0 name="required"
key="interhelp.updatemembersecurity.answer"/>
<msg name="maxlength" key="interhelp.error.field.length"/>
<arg0 name="maxlength"
key="interhelp.updatemembersecurity.answer"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>16</var-value>
</var>
</field>
<field property="question" depends="required, maxlength">
<msg name="required" key="interhelp.error.field.required"/>
<arg0 name="required"
key="interhelp.updatemembersecurity.question"/>
<msg name="maxlength" key="interhelp.error.field.length"/>
<arg0 name="maxlength"
key="interhelp.updatemembersecurity.question"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>40</var-value>
</var>
</field>
</form>

<validator name="comparewithotherfield"
classname="nl.xxxx.pd.appl.form.validator.FormFieldValidator"
method="validateEqualToField"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
msg="interhelp.error.field.compare"/>

public static boolean validateEqualToField(
Object bean,
ValidatorAction va,
Field field,
ActionErrors errors,
HttpServletRequest request) {

String value = ValidatorUtil.getValueAsString(bean,
field.getProperty());
String sProperty2 = field.getVarValue("comparewithotherfield");
String value2 = ValidatorUtil.getValueAsString(bean, sProperty2);

if (!GenericValidator.isBlankOrNull(value)) {
try {
if (!value.equals(value2)) {
errors.add(
field.getKey(),
Resources.getActionMessage(request, va, field));

return false;
}
} catch (Exception e) {
errors.add(
field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}
}

return true;
}

By the way, the ServletContext parameter (as seen in the example on
http://struts.apache.org/userGuide/dev_validator.html) isn't present
on any of the validators (the ones that do work) in our application.
Adding it anyway (I tried) doesn't make any difference.

Thanks,
Edwin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top